How to set and get fields in struct's method
问题 After creating a struct like this: type Foo struct { name string } func (f Foo) SetName(name string){ f.name=name } func (f Foo) GetName string (){ return f.name } How do I create a new instance of Foo and set and get the name? I tried the following: p:=new(Foo) p.SetName("Abc") name:=p.GetName() fmt.Println(name) Nothing gets printed, because name is empty. So how do I set and get a field inside a struct? 回答1: Commentary (and working) example: package main import "fmt" type Foo struct { name