Slice content erased after function call : <p>here is a golang behavior that I am trying to understand and change: I wrote a method to populate a structure with slices in Golang. It works within the method itself, but the slice content gets lost outside of the method. I want however to keep the content. It probably comes from the fact that the pointers inside the slice where deleted at the end of the populateslice method, but how should I write it to prevent this to happen, ie. keep the content in mystruct.myslice after the function call ?</p>

<p>Here is how I wrote the code:</p>

<pre><code>type BBDatacolumn struct {
  Data []string
}

type Mystruct struc {
   myslice []BBDatacolumn
}

//Method to populate the slice of the structure mystruct:
func (self mystruct) populateslice() {
   for i:=0; i&lt;imax; i++ {
     bufferdatacolumn := NewBBDatacolumn()
     //Here, code to populate bufferdatacolumns

     self.myslice = append(self.myslice, bufferdatacolumn)
  }

  self.myslice.display() //Here, works fine: myslice contains the data of the BBDatacolumn correctly
}

//Later in the code (outside of the populateslice func):
mystructinstance.populateslice() //Populates slice OK at the end of the function
mystructinstance.display() //Problem: mystructinstance.myslice is empty: Instanciation of Mystruct does not contain the data in myslice anymore as it did inside the populateslice method
</code></pre>
