标签:mutex nbsp 资源 rgb func print import test rgba
package main //求1-200的阶乘 //使用go里面协程提高速度 //加锁是因为有资源竞争,所以加锁 import ( "fmt" "sync" "time" ) var ( myMap=make(map[int]int,10); lock sync.Mutex; ) func test(n int){ res:=1; for i:=1;i<=n ; i++ { res+=i; } lock.Lock(); myMap[n]=res; lock.Unlock(); } func main(){ for i:=1;i<200;i++ { go test(i); } time.Sleep(time.Second*10); lock.Lock(); for k,v:=range myMap { fmt.Printf("map[%d]=%d\n",k,v); } lock.Unlock(); }
标签:mutex nbsp 资源 rgb func print import test rgba
原文地址:https://www.cnblogs.com/zh718594493/p/14147175.html