I'm trying to achieve parallel processing and communication over the channels in go.
What I basically try to solve is process a specifc data in parallel, and get results in order => introduced type Chunk
for the purpose (see bellow).
I just make new channel for each chunk processing and keep them in slice => expect to be ordered once I iterate over them afterwards.
Simplified version of my program is (https://play.golang.org/p/RVtDGgUVCV):
package main
import (
"fmt"
)
type Chunk struct {
from int
to int
}
func main() {
chunks := []Chunk{
Chunk{
from: 0,
to: 2,
},
Chunk{
from: 2,
to: 4,
},
}
outChannels := [](<-chan struct {
string
error
}){}
for _, chunk := range chunks {
outChannels = append(outChannels, processChunk(&chunk))
}
for _, outChannel := range outChannels {
for out := range outChannel {
if out.error != nil {
fmt.Printf("[ERROR] %s", out.error)
return
}
fmt.Printf("[STDOUT] %s", out.string)
}
}
}
func processChunk(c *Chunk) <-chan struct {
string
error
} {
outChannel := make(chan struct {
string
error
})
go func() {
outChannel <- struct {
string
error
}{fmt.Sprintf("from: %d to: %d
", c.from, c.to), nil}
close(outChannel)
}()
return outChannel
}
The output I see is:
[STDOUT] from: 2 to: 4
[STDOUT] from: 2 to: 4
What I'd however expect to see would be:
[STDOUT] from: 0 to: 2
[STDOUT] from: 2 to: 4
What am I doing wrong here? I don't see it.