package main import ( "fmt" "sync" "time" ) //定义一个全局变量 var count int //创建互斥锁 var mu sync.Mutex //模拟多个线程同时对 count 加 1 func add() { mu.Lock() t := count time.Sleep(time.Second) t++ count = t mu.Unlock() } func main() { for i := 0; i < 5; i++ { go add() } time.Sleep(time.Second * 3) fmt.Println("result:", count) }