Thread synchronization
Go idiomatic way
- Example of thread synchronization using channels
package main
import (
"fmt"
)
func main() {
params := []map[string]any{
{"cloud": "aws"},
{"cloud": "gcp"},
{"cloud": "azure"},
{"cloud": "oracle"},
}
responseChan := make(chan map[string]any, len(params))
for i, param := range params {
go func(i int, p map[string]any) {
p["id"] = i
httpRequest(p, responseChan)
}(i, param)
}
for i := 0; i < len(params); i++ {
response := <- responseChan
fmt.Println(response["id"], response["cloud"])
}
close(responseChan)
}
func httpRequest(source map[string]any, responseChan chan map[string]any) {
responseChan <- source
}
Traditional way
- Example of thread synchronization using WaitGroup
package main
import (
"fmt"
"sync"
)
func main() {
params := []map[string]any{
{"cloud": "aws"},
{"cloud": "gcp"},
{"cloud": "azure"},
{"cloud": "oracle"},
}
responseChan := make(chan map[string]any, len(params))
var wg sync.WaitGroup
for i, param := range params {
wg.Add(1)
go func(i int, p map[string]any) {
defer wg.Done()
p["id"] = i
httpRequest(p, responseChan)
}(i, param)
}
go func() {
wg.Wait()
close(responseChan)
}()
for response := range responseChan {
fmt.Println(response["id"], response["cloud"])
}
}
func httpRequest(source map[string]any, responseChan chan map[string]any) {
responseChan <- source
}