import "fmt"
type Animal interface {
Talk()
Move()
}
type Dog interface {
Animal
}
type Shepherd struct {
Dog
}
func New() Animal {
return &Shepherd{}
}
func (p *Shepherd) Talk() {
fmt.Println("woof!")
}
func main() {
fmt.Println("Hello, playground")
fido := New()
fido.Talk()
fido.Move()
}
woof!panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x20 pc=0x1023e57]
goroutine 1 [running]:
main.(*Shepherd).Move(0xc010036160)
/tmpfs/gosandbox-3b4927a9_89b3d8f1_41a3ab00_1dfeee01_401631ac/prog.go:1 +0x27
main.main()
/tmpfs/gosandbox-3b4927a9_89b3d8f1_41a3ab00_1dfeee01_401631ac/prog.go:30 +0x85
[process exited with non-zero status]
http://play.golang.org/p/m4ehAN2xFR
But, when I remove the Dog interface from the Shepherd struct (http://play.golang.org/p/BDfYULmjU4):
type Shepherd struct {
// Dog
}
It gives me a compile error as expected:
prog.go:19: cannot use Shepherd literal (type *Shepherd) as type Animal in return argument:
*Shepherd does not implement Animal (missing Move method)
[process exited with non-zero status]
In other words: what exactly happens when I implement an interface in a struct? Is this a bug or expected behavior?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.