I have a package in which I have two interfaces
package main
type A interface {
Close()
}
type B interface {
Connect() (A, error)
}
I have also two structures which implements these interfaces
type C struct {
}
func (c *C) Close() {
}
type D struct {
}
func (d *D) Connect() (*C, error) {
c := new(C)
return c, nil
}
Next I have a function which as a parameter wants an object which implements interface B
func test(b B) {
}
Finally, at the main() function I create D structure object and want to call test() function
func main() {
d := new(D)
test(d)
}
If I try to build that package I have an error.
cannot use d (type *D) as type B in argument to test: *D does not implement B (wrong type for Connect method) have Connect() (*C, error) want Connect() (A, error)
It is simple example of my code where I use external package and want to mock structures for tests. Is it any solution to use interfaces instead of types?