Created
June 25, 2018 03:13
-
-
Save wudi/ef951ad9be2d570186b2b797912adfc6 to your computer and use it in GitHub Desktop.
Test channel bug
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" | |
| "math/rand" | |
| "sync" | |
| ) | |
| func main() { | |
| chs := make(chan int, 10) | |
| var wg sync.WaitGroup | |
| wg.Add(10) | |
| for i := 0; i < 5; i++ { | |
| go func(r int) { | |
| defer wg.Done() | |
| num := <-chs | |
| fmt.Printf("Routine: %d Recv: %d\n", r, num) | |
| }(i) | |
| } | |
| for i := 0; i < 5; i++ { | |
| go func(r int) { | |
| defer wg.Done() | |
| num := rand.Intn(10) | |
| chs <- num | |
| fmt.Printf("Routine: %d Send: %d\n", r, num) | |
| }(i) | |
| } | |
| wg.Wait() | |
| fmt.Println("done") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
test