I try to write a function that validate data. Look at the following code:
func Create(name, email, password, local string, termOf bool) map[string]string {
wait := new(sync.WaitGroup)
mutex := new(sync.Mutex)
errMsg := make(map[string]string)
if !termOf {
mutex.Lock()
errMsg["termOf"] = translate(local, "text06")
mutex.Unlock()
}
wait.Add(1)
go func() {
err := ValidateName(name, local)
mutex.Lock()
errMsg["name"] = err.Error()
mutex.Unlock()
wait.Done()
}()
wait.Add(1)
go func() {
err := ValidateEmail(email, local)
mutex.Lock()
errMsg["email"] = err.Error()
mutex.Unlock()
wait.Done()
}()
wait.Add(1)
go func() {
err := ValidatePassword(password, local)
mutex.Lock()
errMsg["password"] = err.Error()
mutex.Unlock()
wait.Done()
}()
wait.Wait()
// If errors appear
if len(errMsg) > 0 {
return errMsg
}
return nil
}
As you can see here, I use three goroutines and in the goroutine I lock it to change errMsg variable map type. When I run the function, I've got compiler error
runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x14 pc=0x44206a]
But when I remove in the goroutine all errMsg inserts, then the function works. I do not know the reason what I do wrong.