This is the basic concept of the Go channels (or other CSPs , such as Clojure core.async ) that they block. In general, as you already mentioned, there are two types of channels:
- buffered, which blocks if the buffer is full.
- unbuffered block, if not rendezvous, i.e. there must be someone who puts (
c <- ), and someone who takes ( <- c ) from the channel.
In your particular case, the Go runtime is smart enough to detect that none of those who ever take 3 from channel c . Therefore, it is deadlock and (fortunately) an error is thrown.
What you usually do when working with channels is goroutines (checkout is an introduction ), which spawns a light thread controlled by Go runtime - simultaneously executing the body:
c := make(chan int) go func() { c <- 3 }() // Create a new gorountine that puts 3 to the channel fmt.Println(<- c) // Take 3 from the channel and print it in the main thread
beatngu13
source share