title: How to save data in Go struct from function/method?
body: <p>I'm just starting out with Go and I'm having a hard time saving data in a <code>struct</code>. Coming from other languages I learned there is no such thing as a <code>class</code> in Go. For similar purposes, the <code>struct</code> can be used, and functions can be "added" to the struct. So I wrote the following simple program:</p>
  
  <pre><code>package main
  
  import "fmt"
  
  type MyStruct struct {
      the_number int
  }
  func (self MyStruct) add(another_number int) int {
      self.the_number += another_number  // I save the result to the struct the_number
      return self.the_number
  }
  
  func main() {
      my_struct := MyStruct{1}
      result := my_struct.add(2)
      fmt.Println(result)               // prints out 3 as expected
      fmt.Println(my_struct.the_number) // prints out 1. Why not also 3?
  }
  </code></pre>
  
  <p>As you can see from the comments I'm puzzled by the fact that the result is not saved in <code>self.the_number</code> in the instantiated <code>my_struct</code>.</p>
  
  <p>So I found out I can get around this by doing</p>
  
  <pre><code>my_struct.the_number = my_struct.add(2)
  </code></pre>
  
  <p>But I methods/functions can sometimes become complex in which I want to save a lot of data to the <code>my_struct</code> from within the function.</p>
  
  <p>Could any smarter soul than me give me a tip on what I'm missing here?</p>
  
  <p>How can I save data to an instantiated <code>struct</code> from within a function?</p>