Search⌘ K

Solution Review: Demonstrate the Blocking Nature

Explore how Go's goroutines and channels work together to manage concurrency. This lesson demonstrates the blocking nature of channels, showing how main waits for data from a goroutine before continuing execution, solidifying your understanding of Go's synchronization mechanism.

We'll cover the following...
Go (1.6.2)
package main
import "fmt"
import "time"
func main() {
c := make(chan int)
go func() {
time.Sleep(5 * 1e9)
fmt.Println("received", <- c) // value recieved from channel
}()
fmt.Println("sending", 10)
c <- 10 // putting 10 on the channel
fmt.Println("sent", 10)
}

At line 6, we make a channel c for integers. Then, from line 7 to line 10, we start a ...