The translation of the material was prepared as part of the "Golang Developer. Professional" course . If you are interested in learning more about the course, we invite you to an online open house .
️ This article is based on Go 1.14.
Go , (channel) (mutex ), . , . , , Go atomic. , .
(data race) , , . map , , .
, , . :
, - :
[...]
&{[79167 79170 79173 79176 79179 79181]}
&{[79216 79219 79220 79221 79222 79223]}
&{[79265 79268 79271 79274 79278 79281]}
, , . -race
:
WARNING: DATA RACE
Read at 0x00c0003aa028 by goroutine 9:
[...]
fmt.Printf()
/usr/local/go/src/fmt/print.go:213 +0xb5
main.main.func2()
main.go:30 +0x3b
Previous write at 0x00c0003aa028 by goroutine 7:
main.main.func1()
main.go:20 +0xfe
( ) atomic.
Mutex vs Atomic
sync
: sync.Mutex
sync.RWMutex
; , . :
; :
[...]
&{[213 214 215 216 217 218]}
&{[214 215 216 217 218 219]}
&{[215 216 217 218 219 220]}
atomic
. :
:
[...]
&{[32724 32725 32726 32727 32728 32729]}
&{[32733 32734 32735 32736 32737 32738]}
&{[32753 32754 32755 32756 32757 32758]}
, , atomic , . , .
, . , , , , . , , , , . :
:
name time/op
AtomicOneWriterMultipleReaders-4 72.2ns ± 2%
AtomicMultipleReaders-4 65.8ns ± 2%
MutexOneWriterMultipleReaders-4 717ns ± 3%
MutexMultipleReaders-4 176ns ± 2%
, . , , .
trace «Go: Discovery of the Trace Package.».
, atomic
:
. , :
, , . , , :
. :
atomic
. . , map, map, .
"Golang Developer. Professional"
- « »