title: goroutines always execute "last in first out"
body: <p>in the interests of learning more about Go, I have been playing with goroutines, and have noticed something - but am not sure what exactly I'm seeing, and hope someone out there might be able to explain the following behaviour.</p>
  
  <p>the following code does exactly what you'd expect:</p>
  
  <pre><code>package main
  
  import (
    "fmt"
  )
  
  type Test struct {
    me int
  }
  
  type Tests []Test
  
  func (test *Test) show() {
    fmt.Println(test.me)
  
  }
  
  func main() {
    var tests Tests
    for i := 0; i &lt; 10; i++ {
      test := Test{
        me: i,
      }
      tests = append(tests, test)
    }
  
    for _, test := range tests {
      test.show()
    }
  
  }
  </code></pre>
  
  <p>and prints 0 - 9, in order.</p>
  
  <p>now, when the code is changed as shown below, it always returns with the last one first - doesn't matter which numbers I use:</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "sync"
  )
  
  type Test struct {
      me int
  }
  
  type Tests []Test
  
  func (test *Test) show(wg *sync.WaitGroup) {
      fmt.Println(test.me)
      wg.Done()
  
  }
  
  func main() {
      var tests Tests
      for i := 0; i &lt; 10; i++ {
          test := Test{
              me: i,
          }
          tests = append(tests, test)
      }
  
      var wg sync.WaitGroup
      wg.Add(10)
      for _, test := range tests {
          go func(t Test) {
              t.show(&amp;wg)
          }(test)
      }
      wg.Wait()
  
  }
  </code></pre>
  
  <p>this will return:
  9
  0
  1
  2
  3
  4
  5
  6
  7
  8</p>
  
  <p>the order of iteration of the loop isn't changing, so I guess that it is something to do with the goroutines...
  basically, I am trying to understand why it behaves like this...I understand that goroutines can run in a different order than the order in which they're spawned, but, my question is why this always runs like this. as if there's something really obvious I'm missing...</p>