My Go project breaks when I try to write concurrently.
I have a map map[string][]strings
. I want to write files concurrently, which are named as key + ".csv"
and for each value of []strings
write another row to the file with the value in it.
What I've done:
for key, val := range varValMap {
go writeCsv(key, val)
}
And the func is:
func writeCsv(variable string, values []string) {
fName := variable + ".csv"
fPath := filepath.Join(xmlFolder, "_varval", fName)
file, err := os.Create(fPath)
check(err)
defer file.Close()
for _, value := range values {
fmt.Fprintf(file, value + "
")
}
}
The program finishes with no problems detectable by me, yet with no complete result (some files are written but nothing in them), also when I remove go
from go writeCsv(key, val)
all is well in go land.
You're not waiting for your goroutines to finish. This means that as soon as your for loop is done launching all the goroutines, the program exits. Any goroutine still running is aborted.
You can use a sync.WaitGroup as a simple way to wait for execution to be finished:
var wg sync.WaitGroup
for key, val := range varValMap {
wg.Add(1)
go writeCsv(&wg, key, val)
}
wg.Wait()
And change your writeCsv
to notify the wait group:
func writeCsv(wg *sync.WaitGroup, variable string, values []string) {
defer wg.Done()
// do everything.
}