Skip to content

Instantly share code, notes, and snippets.

@sotex
Created May 3, 2020 13:41
Show Gist options
  • Save sotex/b039738bfccca3223c5f94b1adb58b60 to your computer and use it in GitHub Desktop.
Save sotex/b039738bfccca3223c5f94b1adb58b60 to your computer and use it in GitHub Desktop.
Go 并发控制
package main
import "log"
import "time"
import "sync"
var(
ch chan int
wg sync.WaitGroup
)
func init(){
ch = make(chan int,4)
}
func main() {
for i:=0; i<10; i++ {
ch <- i
wg.Add(1)
go func(x int){
defer func(){
i := <- ch
log.Println("i = ",i)
wg.Done()
}()
log.Println("休眠前",x)
time.Sleep(time.Second)
}(i)
}
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment