title: Assign values from slice of unknown length to struct in Go?
body: <p>I find the case below from some useful packages in github and it looks so ugly and stupid. I think the better design of code can avoid this, but if we meet this, can it be written more concisely?</p>
  <p>Is there a better way to do that instead of continuous checking by slice length?</p>
  <pre class="lang-golang prettyprint-override"><code>package main
  
  type NumberSet struct {
      SetName string
      Number1 int
      Number2 int
      Number3 int
  }
  
  func main() {
      set := &amp;NumberSet{SetName: &quot;test&quot;}
      var numbers []int
      // assume that we don't know the actual length of slice
      numbers = append(numbers, 1)
      numbers = append(numbers, 2)
  
      length := len(numbers)
      if length &gt; 0 {
          set.Number1 = numbers[0]
      }
      if length &gt; 1 {
          set.Number2 = numbers[1]
      }
      if length &gt; 2 {
          set.Number3 = numbers[2]
      }
  }
  </code></pre>