I’m using WaitGroup to concurrently populate a struct instance. Each goroutine will populate separate fields of the struct. Do I have to lock the struct instance before using it?
package main
import (
"fmt"
"sync"
)
type st struct {
field1 string
field2 string
field3 string
}
func main() {
var wg sync.WaitGroup
wg.Add(3)
s := &st{}
go func() {
defer wg.Done()
s.field1 = "asdf"
}()
go func() {
defer wg.Done()
s.field2 = "qwert"
}()
go func() {
defer wg.Done()
s.field3 = "zxcv"
}()
wg.Wait()
fmt.Println("%#v", s)
}