Channels and select
This file contains 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
// :collection Essential Go | |
package main | |
import ( | |
"fmt" | |
"math/rand" | |
) | |
// :show start | |
func genInts(chInts chan int) { | |
chInts <- rand.Intn(1000) | |
} | |
func main() { | |
chInts := make(chan int) | |
for i := 0; i < 2; i++ { | |
go genInts(chInts) | |
} | |
n := <-chInts | |
fmt.Printf("n: %d\n", n) | |
select { | |
case n := <-chInts: | |
fmt.Printf("n: %d\n", n) | |
} | |
} | |
// :show end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment