I have encountered a problem in Golang as bellow:
package main
import "fmt"
type Foo struct {
name string
}
type Bar struct{
Foo
id string
}
func (f *Foo) SetName(name string) {
f.name = name
}
func (f *Foo) Name() string {
return f.name
}
func main(){
f := &Foo{}
f.SetName("Set Foo name")
fmt.Println("Get from Foo struct name: ", f.Name() )
bar := &Bar{
Foo:Foo{name: "Set Foo name from Bar struct!"},
id: "12345678",
}
fmt.Println("Bar setName(): ", bar.SetName("New value set to Foo struct name") )
fmt.Println("Bar getName(): ", bar.Name())
}
Results:
./struct-2.go:33: bar.Foo.SetName("New value set to Foo struct name") used as value
But if I comment out this line then I can get the bar.Name() method works.
// fmt.Println("Bar setName(): ", bar.SetName("New value set to Foo struct name") )
Why I got that error for bar.SetName() method? Thanks.
SetNamedoes not return anything, and you're trying to pass this "nothing" tofmt.Println. Which is impossible.