How to set and get fields in struct's method

Viewed 104123

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?

Working playground

3 Answers
Related