title: Go - append to slice on struct does not persist
body: <p>I have a hard time understanding what seems to be a pretty basic operation. I want to create two structs, where one of the structs will hold a slice of another struct. Here' a simple example to illustration the problem:</p>
  
  <pre><code>// Post structure
  type Post struct {
      User string `bson:"user"`
      Body string `bson:"body"`
  }
  
  // User structure
  type User struct {
      Name  string `bson:"name"`
      Posts []Post `bson:"posts"`
  }
  
  func (u *User) appendPost(post Post) []Post {
      u.Posts = append(u.Posts, post)
      return u.Posts
  }
  
  func main() {
      p1 := Post{User: "Jane", Body: "First Jane's post"}
      u := User{Name: "Jane"}
  
      users := []User{}
      users = append(users, u)
  
      for _, user := range users {
          user.appendPost(p1)
      }
  
      // [{Jane []}]
      fmt.Println(users)
  }
  </code></pre>
  
  <p>This code produces no errors and also no effect. If I initialize and append a user with the predefined post like this: <code>u := User{Name: "Jane", Posts: []Post{p1}}</code> - everything works as expected. But I can't figure out why the same operation doesn't work in a two steps:
  1. Create user with only Name and later append posts in a for loop. Thanks in advance.</p>
  
  <p><a href="https://play.golang.org/p/SAiofI26x9" rel="noreferrer">This code on the playground</a></p>