Async/Await-like pattern in Go to make structured concurrency easy and with less boilerplate
disclaimer:
It is just a pattern that wraps Goroutine and channel. It is still Goroutine underneath. I am not bringing "coloring" to Go code. Functions still call other functions as normal functions, without the need to distinguish async/sync ones.
Goroutine
For beginners of Go who come from other "colored" languages like JavaScript, one big difference in Golang's mental model is Goroutine. Unlike JavaScript, where you have to be inside an Async function when you want to invoke another Async one, in Go, you can fire a function that runs in parallel anywhere in your function code with the help of Goroutine.
func Add () int { a := 1 b := 2 var c int doneSignal := make ( chan struct {}, 1 ) go func () { // fire a function running in parallel with Goroutine c = 3 // able to edit variable outside the local scope go func () {}() // able to start child Goroutine inside a goroutine doneSignal <- struct {}{} }() <-…