Channels are a reference type that provide a safe mechanism to share data between goroutines. Unbuffered channel give a guarantee of delivery that data has passed from one goroutine to the other. Buffered channels allow for data to pass through the channel without such guarantees. Unbuffered channels require both a sending and receiving goroutine to be ready at the same instant before any send or receive operation can complete. Buffered channels don't force goroutines to be ready at the same instant to perform sends and receives.
- Unbuffered channels provide a guarentee that data has been exchanged at some instant.
- Buffered channels provide great support for managing goroutines and resources.
- Closed channels can provide a system wide mechanism for notifications.
- A send on an unbuffered channel happens before the corresponding receive from that channel completes.
- A receive from an unbuffered channel happens before the send on that channel completes.
- The closing of a channel happens before a receive that returns a zero value because the channel is closed.
http://blog.golang.org/pipelines
http://blog.golang.org/share-memory-by-communicating
http://www.goinggo.net/2014/02/the-nature-of-channels-in-go.html
Unbuffered channels - Tennis game (Go Playground)
Unbuffered channels - Relay race (Go Playground)
Buffered channels - Manage concurrency (Go Playground)
Timer channels and Select (Go Playground)
Write a program where two goroutines pass an integer back and forth ten times. Display when each goroutine receives the integer. Increment the integer with each pass. Once the interger equals ten, terminate the program cleanly.
Write a program that uses a buffered channel to maintain a buffer of four strings. In main, send the strings 'A', 'B', 'C' and 'D' into the channel. Then create 20 goroutines that receive a string from the channel, display the value and then send the string back into the channel. Once each goroutine is done performing that task, allow the goroutine to terminate.