Created
October 4, 2017 14:13
-
-
Save samuell/0213ef3026fb8a344ccfe1d486f87981 to your computer and use it in GitHub Desktop.
Run this on https://play.golang.org/p/YvlP2ETJDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
bufSize := 1 // Try changing this to 0, and see what happens! | |
ch1, ch2, ch3 := make(chan int, bufSize), make(chan int, bufSize), make(chan int, bufSize) | |
go func() { | |
defer close(ch1) | |
defer close(ch2) | |
defer close(ch3) | |
ch1 <- 1 | |
ch2 <- 2 | |
ch3 <- 3 | |
ch1 <- 4 | |
ch2 <- 5 | |
ch3 <- 6 | |
}() | |
// This is the nice and succinct way to read one item each from a set of channels, | |
// which I like: | |
a, b, c := <-ch1, <-ch2, <-ch3 | |
fmt.Println(a, b, c) | |
// And, to demonstrate that the order in which we receive items is not important, we are | |
// receiving in the reverse order compared with the order in which they were sent, | |
// which works because of bufSize >= 1: | |
c, b, a = <-ch3, <-ch2, <-ch1 | |
fmt.Println(a, b, c) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment