« Back to Index

Flawed Golang concurrency logic: diff below shows the fixed code

View original Gist on GitHub

Flawed Golang concurrency logic.diff

func someFunction {
-	ch := make(chan aggregator.ComponentResponse)
+	ch = make(chan aggregator.ComponentResponse, len(components.Components))

 	for i, v := range components.Components {
 		wg.Add(1)
		go getComponent(i, v)
-		cr = append(cr, <-ch)
 	}
 	wg.Wait()
+	close(ch)
+
+	for component := range ch {
+		cr = append(cr, component)
+	}