Still new to Go. I'm trying to implement the answer as suggested here to my previous question. In this case I have an animal interface and a bunch of animal structs. I want to be able to iterate over each animal & get it's language. I've tried a list of pointers but I keep getting an error "y.language undefined (type animal has no field or method language)":
package main
import "fmt"
type animal interface {
speak()
}
type dog struct{
language string
}
func (d *dog) speak() {
d.language = "woof"
}
var n = []animal{
&dog{},
}
func main() {
for _, w := range n{
x := &dog{}
fmt.Println(x)
x.speak()
fmt.Println(x.language) // this works
fmt.Println(w)
y := w
y.speak()
fmt.Println(y.language) // but this doesn't...why??
}
}
EDIT #1: To make my question clearer: Why does x := &dog{} work but y := w does not? EDIT #2: No love for newbs ;(