Go语言中文网 为您找到相关结果 2

sync.mutex

golang的多线程固然好用,但是有时候需要对数据进行上锁,防止数据被其它线程更改。那么sync包下的Mutex非常好用。 Mutex是一个互斥锁。可以作为struct的一部分,这样这个struct就会防止被多线程更改数据。 来个例子: package main import ( "fmt" "sync" "time" ) type User struct { Name string Locker *sync.Mutex } func (u *User) SetName(wati *sync.WaitGroup, name string) { defer func() { fmt.Println("Unlock set name:", name) u.Locker.Unlock() wati...阅读全文

博文 2018-07-06 10:34:48 哆啦在这A梦在哪