package main import ( "fmt" "sync" "time" ) //协程并发控制,只执行一次 var num = 0 var once = sync.Once{} func main() { for i := 0; i <= 100; i++ { go once.Do(addNum) } time.Sleep(time.Second*10) fmt.Println(num) } func addNum() { num = num + 1 }
package main import ( "fmt" "sync" "time" ) //协程并发控制,只执行一次 var num = 0 var once = sync.Once{} func main() { for i := 0; i <= 100; i++ { go once.Do(addNum) } time.Sleep(time.Second*10) fmt.Println(num) } func addNum() { num = num + 1 }