November 2017
Intermediate to advanced
670 pages
17h 35m
English
In order to increase performance, we should consider running things concurrently. Go has a few concurrency constructs that we can use: Goroutines and channels. Let's give that a try:
func main() { input := make(chan Order) output := make(chan Order) go func() { for order := range input { output <- Pipeline(order) } }() orders := GetOrders() for _, order := range orders { fmt.Printf("Processed order: %v\n", Pipeline(*order)) } close(input) }
We created an input channel and an output channel for our pipeline.
Next, we created an immediately executable Goroutine function. Note the open/close parenthesis at the end of the Goroutine block: }() . This Goroutine won't exit until we close the input channel in the last line ...