Given the following Go code example:
package main
import "fmt"
type greeter interface {
hello()
goodbye()
}
type tourGuide struct {
name string
}
func (t tourGuide) hello() {
fmt.Println("Hello", t.name)
}
func (t *tourGuide) goodbye() {
fmt.Println("Goodbye", t.name)
}
func main() {
var t1 tourGuide = tourGuide{"James"}
t1.hello() // Hello James
t1.goodbye() // Goodbye James (same as (&t1).goodbye())
var t2 *tourGuide = &tourGuide{"Smith"}
t2.hello() // Hello Smith
t2.goodbye() // Goodbye Smith (same as (*t2).hello())
// illegal: t1 is not assignable to g1 (why?)
// var g1 greeter = t1
var g2 greeter = t2
g2.hello() // Hello Smith
g2.goodbye() // Goodbye Smith
}
I'm able to call the two methods of the struct tourGuide
using either a variable of type tourGuide t1
or a pointer to tourGuide t2
. In other words, I can call a method with T
receiver using a variable of type T
or *T
. Similarly, I can call a method with *T
receiver using a variable of type T
(if T
is addressable) or *T
. I understand that the compiler handles the differences here (see my comments in the code).
However, things change when we are implementing interfaces. In the above code, a variable of type greeter
interface is assignable from a pointer to tourGuide
but not from a tourGuide
.
Can anyone tell me why this is the case? Why am I able to call t1.hello()
and t1.goodbye()
but somehow t1
is not enough for the interface greeter
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…