import "sync"type SyncedData struct { inner map[string]int mutex sync.Mutex}func (s *SyncedData) Insert(k string, v int) { s.mutex.Lock() defer s.mutex.Unlock() s.inner[k] = v}func (s *SyncedData) Get(k string) int { s.mutex.Lock() defer s.mutex.Unlock() return s.inner[k]}// now these functions will work with multiple goroutines
Wait groups
increment a counter when a routine is added, decrement when it finishes
var wg sync.Waitgroupsum := 0for i := 0; i < 20; i++ { wg.Add(1) value += i go func() { defer wg.Done() sum += value }()}wg.Wait()fmt.Println(sum)