Quantcast
Channel: How to set and get fields in struct's method - Stack Overflow
Viewing all articles
Browse latest Browse all 4

How to set and get fields in struct's method

$
0
0

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


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>