I’m new to Go and a bit confused in how interfaces work when implemented, especially wrt. functions that have parameters. Say I have an interface with function foo
that takes in an interface:
type I interface {
foo(x interface{})
}
And a types A
and B
as below:
type A struct {
// stuff
}
type B struct {
// stuff
}
(a *A) foo(b B) {
// do something
}
Since, in this example, A
has method foo
, is it considered to have implemented I
, even with the specification of B
as the param rather than a general interface? I have the same question for the function’s return types as well.
If not, how would one design an interface which allows a more general parameter/return type and for structs which implement it to specify the type?