Skip to content

Instantly share code, notes, and snippets.

@wudi
Created June 25, 2018 03:13
Show Gist options
  • Select an option

  • Save wudi/ef951ad9be2d570186b2b797912adfc6 to your computer and use it in GitHub Desktop.

Select an option

Save wudi/ef951ad9be2d570186b2b797912adfc6 to your computer and use it in GitHub Desktop.
Test channel bug
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")
}
@sulwan
Copy link

sulwan commented Jun 25, 2018

您这个是打算反应什么?多携程?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment