title: Golang: override initial elements of a slice
body: <p>I want to initialize an empty integer slice of length 3, store random ints in the slice, and sort the slice at each iteration. This code accomplishes that:</p>
  <pre><code>package main
  
  import (
      &quot;fmt&quot;
      &quot;math/rand&quot;
      &quot;sort&quot;
  )
  
  func main() {
      myslice := make([]int, 3)
      for i := 0; i &lt; 10; i++ {
          myslice = append(myslice, rand.Intn(100))
          sort.IntSlice(myslice).Sort()
          fmt.Println(myslice)
      
      }
  }
  </code></pre>
  <p>End result: <code>[0 0 0 5 23 28 43 45 50 57 68 76 87]</code> However, I would also like to override the initial elements (in other words, get rid of those zeroes) before appending new elements. Since the slice is sorted at each iteration, calling <code>myslice[i] = rand.Intn(100)</code> if <code>i &lt; 3</code> wouldn't necessarily work.</p>
  <p>My initial solution is to write a function to get the first index of zero for <code>i &lt; 3</code>, and replace that index with a new random value... however, I was wondering whether there is a better option to override the initial values of a slice.</p>