GitBucket
4.23.0
Toggle navigation
Sign in
Files
Branches
1
Releases
Issues
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
sample-golang
/
01_hello
Browse code
add select sample
master
1 parent
be7f3f9
commit
ad3a7dba6b25303d8c84e8ca9b0f066352197d0d
yhornisse
authored
on 9 Oct 2021
Patch
Showing
1 changed file
goroutine/select.go
Ignore Space
Show notes
View
goroutine/select.go
0 → 100644
package main import ( "fmt" ) func main() { ch1 := make(chan int) ch2 := make(chan int) for i := 0; i < 5; i++ { if i % 2 == 0 { go func(i int) { ch1 <- i }(i) } else { go func(i int) { ch2 <- i }(i) } } cnt := 0 df := 0 for cnt < 5 { select { case <- ch1: fmt.Printf("ch1 received\n") cnt++ case v := <- ch2: fmt.Printf("ch2: %d\n", v) cnt++ default: df++ } } fmt.Printf("default: %d\n", df) }
Show line notes below