title: Unbuffered channel in Go
body: <p>Here is a simple example code about unbuffered channels:</p>
  
  <pre><code>ch01 := make(chan string)
  
  go func() {
      fmt.Println("We are in the sub goroutine")
      fmt.Println(&lt;-ch01)
  }()
  
  fmt.Println("We are in the main goroutine")
  ch01 &lt;- "Hello"
  </code></pre>
  
  <p>The result I got:</p>
  
  <pre><code>We are in the main goroutine 
  We are in the sub goroutine 
  Hello
  </code></pre>
  
  <p>Go playground:
  <a href="https://play.golang.org/p/rFWQbwXRzGw" rel="nofollow noreferrer">https://play.golang.org/p/rFWQbwXRzGw</a></p>
  
  <p>From my understanding, the send operation blocked the main goroutine, until the sub goroutine executed a receive operation on channel <code>ch01</code>.  Then the program exited.</p>
  
  <p>After placing the sub goroutine after the send operation like that:</p>
  
  <pre><code>fmt.Println("We are in the main goroutine")
  ch01 &lt;- "Hello"
  
  go func() {
      fmt.Println("We are in the sub goroutine")
      fmt.Println(&lt;-ch01)
  }()
  </code></pre>
  
  <p>A deadlock occurred:</p>
  
  <pre><code>We are in the main goroutine
  fatal error: all goroutines are asleep - deadlock!
  </code></pre>
  
  <p>go playground
  <a href="https://play.golang.org/p/DmRUiBG4UmZ" rel="nofollow noreferrer">https://play.golang.org/p/DmRUiBG4UmZ</a></p>
  
  <p>What happened this time? Did that mean after <code>ch01 &lt;- "Hello"</code> the main goroutine was immediately blocked so that the sub goroutine had no chance to run? If it is true, how should I understand the result of the first code example?(At first in main goroutine, then in sub goroutine).</p>