I am struggling to understand where I should close my channels.
This bit of code takes about 0.7 seconds:
options := [3]string{"0", "1", "2"}
str := fmt.Sprintf("%6d ", id)
for j := 0; j < 40000; j++ {
str += options[rand.Intn(3)]
}
str += "\n"
Adding an io.Writestring does not make a difference to the time, so the problem is this bit.
I want roughly 100,000 records like these so I thought to put in a goroutine.
func main() {
file, _ := os.Create("myfile.txt")
ch := make(chan string)
for i := 0; i < 100000; i++ {
go generate(i, ch)
}
counter := 0
for result := range ch {
counter++
io.WriteString(file, result)
if counter == 100000 {
close(ch)
}
}
file.Close()
}
func generate(id int, c chan string) {
options := [3]string{"0", "1", "2"}
str := fmt.Sprintf("%6d ", id)
for j := 0; j < 40000; j++ {
str += options[rand.Intn(3)]
}
str += "\n"
c <- str
}
From what I understand is that I am closing the channel on the receiver side, which is not ideal? Also, this way all 100,000 should be sent to goroutines first, before I can receive any. Can I send requests to generate a record and start receiving at the same time?