For example in the following example:
type Food interface {
Eat() bool
}
type vegetable_s struct {
//some data
}
type Vegetable *vegetable_s
type Salt struct {
// some data
}
func (p Vegetable) Eat() bool {
// some code
}
func (p Salt) Eat() bool {
// some code
}
Do Vegetable
and Salt
both satisfy Food
, even though one is a pointer and the other is directly a struct?
The answer is easy to get by compiling the code:
prog.go:19: invalid receiver type Vegetable (Vegetable is a pointer type)
The error is based on the specs requirement of:
The receiver type must be of the form T or *T where T is a type name. The type denoted by T is called the receiver base type; it must not be a pointer or interface type and it must be declared in the same package as the method.
(Emphasizes mine)
The declaration:
type Vegetable *vegetable_s
declares a pointer type, ie. Vegetable
is not eligible as a method receiver.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With