mercredi 10 janvier 2018

Go type-error: struct does not implement interface

// BEGIN: external library

type realX struct {}

type realY struct {}

func (realX) Do() realY {
  return realY{}
}

// END

type A struct {
  a myX
}

type myY interface {}

type myX interface {
  Do() myY
}

func foo (arg1 myY) {
}

func main() {
    foo(realY{})
    x := A{realX{}}
    fmt.Println("Hello, playground")
}

I get:

cannot use realX literal (type realX) as type myX in field value:
    realX does not implement myX (wrong type for Do method)
        have Do() realY
        want Do() myY

From the looks of it, realY implements myY, so why can't I do this? This is making it impossible to write mock unit tests cleanly.

Aucun commentaire:

Enregistrer un commentaire