Go tilbyder to tilgange til concurrency-sikkerhed: channels ("del hukommelse via kommunikation") og sync-pakken (mutexes/låse til at beskytte delt hukommelse direkte). På trods af Gos channel-første filosofi er sync-primitiver ofte det simplere og mere effektive valg til at beskytte delt tilstand.
sync.Mutex — beskyt delt tilstand med en lås
Counter {
mu sync.Mutex
count
}
Increment() {
c.mu.Lock()
c.mu.Unlock()
c.count++
}
