Skip to content

Instantly share code, notes, and snippets.

@samuell
Created October 4, 2017 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuell/0213ef3026fb8a344ccfe1d486f87981 to your computer and use it in GitHub Desktop.
Save samuell/0213ef3026fb8a344ccfe1d486f87981 to your computer and use it in GitHub Desktop.
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