title: Goroutine execution order
body: <p>I'm a beginner of golang and I'm learning the goroutine and channel. Intuitively, I consider a gorountine in golang is essentially a thread running independently. Therefore, if there are more than one goroutine, the execution order won't be guaranteed. Hence, the following code should output "ping" and "pong" in a random order. However, what I observed is that the program yields "ping" and "pong" in turn. Can anyone explain the reason for me? Any reply would be helpful. Thanks!</p>
  
  <p>Another intriguing observation I found is that if I don't call sleep function inside the printer function, the program will output in a random order. </p>
  
  <pre><code>package main
  
  import (
    "fmt"
    "time"
  )
  
  func pinger(c chan string) {
    for i := 0; ; i++ {
      c &lt;- "ping"
    }
  }
  
  func printer(c chan string) {
    for {
      msg := &lt;- c
      fmt.Println(msg)
      time.Sleep(time.Second * 1)
    }
  }
  
  func ponger(c chan string) {
    for i := 0; ; i++ {
      c &lt;- "pong"
    }
  }
  
  func main() {
    var c chan string = make(chan string)
  
    go pinger(c)
    go ponger(c)
    go printer(c)
  
    var input string
    fmt.Scanln(&amp;input)
  }
  </code></pre>