Yes, you could use a map for that and you would have to protect it by a mutex.
I would however suggest that each job contains a result channel where you can input the result of the job. Here a rough sketch of what that would look like:
playground: note that playground only has one core so the numbers will appear in sequence.
package main
import (
"fmt"
"time"
)
type job struct {
a int
b int
chRes chan int
}
func main() {
var chIn = make(chan job, 20)
for i := 0; i < 10; i++ {
go worker(chIn)
}
for i := 0; i < 100; i++ {
go func(i int) {
chRes := make(chan int)
chIn <- job{
a: i,
b: i,
chRes: chRes,
}
fmt.Println(<-chRes)
}(i)
}
// I'm lazy, so here just a sleep so we can see something
time.Sleep(1 * time.Second)
}
func worker(ch chan job) {
for job := range ch {
job.chRes <- job.a + job.b
}
}
-- edit --
If you device to go with a store instead (which kind of goes against the go mantra share memory by communicating
instead of communicate by sharing memory
): there is also a sync.Map
that is already concurrency safe. No mutex needed.