WaitGroup
...
Mutex
type atomicInt int

func (a *atomicInt) increment() {
*a++
}

func (a *atomicInt) get() int {
return int(*a)
}

func main() {
var a atomicInt
a.increment()
go func() {
a.increment()
}()
time.Sleep(time.Second)
fmt.Println(a)
}
有个问题:
==================
WARNING: DATA RACE 数据冲突
Read at 0x00c04204a058 by main goroutine:
main.main()
C:/Users/.../atomic.go:25 +0xd7
Previous write at 0x00c04204a058 by goroutine 6:
main.main.func1()
C:/Users/.../atomic.go:11 +0x5b
Goroutine 6 (finished) created at:
main.main()
C:/Users/.../atomic.go:21 +0xb9

------------------
type atomicInt struct {
value int
lock sync.Mutex
}

func (a *atomicInt) increment() {
a.lock.Lock()
defer a.lock.Unlock()
a.value++
}

func (a *atomicInt) get() int {
a.lock.Lock()
defer a.lock.Unlock()
return a.value
}

func main() {
var a atomicInt
a.increment()
go func() {
a.increment()
}()
time.Sleep(time.Second)
fmt.Println(a.get())
}
Cond
...