Concurrency in Go, an overview.
For more detail, see:
A general overview of concurrency, not bound to a specific programming language: Concurrency
Goroutines
A Goroutine is a lightweight thread, managed by the Go runtime, not the OS.
In some cases, not all statements starting with goused for spawning a Goroutine, are finished, as the main process might be already done.
The main function is a go routine itself.
A go routine is created using the gokeyword:
func someFunc()
go someFunc()
Goroutines are executed independelty. The main function doesn't wait for them. Thus, we need to use wait groups to await the execution of a goroutine.
It is common to separat concurrency logic from business logic:
func main() {
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
say(1, "go is awesome")
}()
Channels
In order to let Goroutines collaborate with each other, one must be able to send information between routines. While the shared memory could be used, this is really hard to manage.
Next chapter: HTTP in Go