In essence, four questions are here for Golang interfaces, with each one slightly harder than the one before it.
import (
"A",
"B",
"C",
// more ...
"G"
)
and we define a type S:
type S struct {
// more ...
}
now if S "implements" one of the interfaces:
func (s S) Foo() {
// more ...
}
Up to date, is there any other way to tell which interface of A - G does S implement, except for looking and searching into interface declarations of A - G? If no, is there a way to explicitly tell S to implement which interface? So far, it appears that the which interface S implemented is inferred.
If, for the same example, B, C, D interfaces all have the Foo() method with the same signature. In this case, which interface does S implement? Does it depend on the order those interfaces are imported from B, C, and D?
If, rather than importing interfaces, we declare and define interfaces B, C, D in the same file, but those interfaces again all have Foo() method with the same signature, which interface does S implement? Does it depend on the order those interfaces are declared, or defined?
If, we declared and defined B, C, D interfaces in the same file, B has Foo() method, C has Bar() method, and D has both Foo() and Bar(). Now if S implement Foo() and Bar(), then does S implement B and C, or S only implements D?
Thanks, everyone!
No. (But as a side note, you don't import interfaces, you import packages.)
All of them. Order doesn't matter. If you declared variables of type B
, C
, and D
, all of them would be able to hold an S
.
All of them. What you do in the same or different files doesn't matter.
All of them. S
implements B
because S
has Foo()
. S
implements C
because S
has Bar()
. S
implements D
because S
has both Foo()
and Bar()
.