Reputation: 1699
I have interface:
type MyInterface interface {
...
}
and I want to mark that my struct implements it. I think it is not possible in go, but I want to be certain.
I did the following, but I think it results in an anonymous variable that implements interface. Am I right?
type MyStruct struct {
...
MyInterface
}
Upvotes: 34
Views: 36542
Reputation: 48114
In Go, implementing an interface is implicit. There is no need to explicitly mark it as implementing the interface. Though it's a bit different, you can use assignment to test if a type implements an interface and it will produce a compile time error if it does not. It looks like this (example from Go's FAQ page);
type T struct{}
var _ I = T{} // Verify that T implements I.
var _ I = (*T)(nil) // Verify that *T implements I.
To answer your second question, yes that is saying your struct is composed of a type which implements that interface.
Upvotes: 74