Go - Goroutine (concurrent function execution)

About

A goroutine is a concurrent function execution. A channel is a communication mechanism that allows one goroutine to pass values of a specified type to another goroutine.

The function main runs in a goroutine and the go statement creates additional goroutines.

When one goroutine attempts a send or receive on a channel, it blocks until another goroutine attempts the corresponding receive or send operation, at which point the value is transferred and both goroutines proceed.

Management

  • To sends a value on the channel ch (ch <- expression)
  • To receives all of them (<-ch).

Having main do all the printing ensures that output from each goroutine is processed as a unit, with no danger of interleaving if two goroutines finish at the same time.

Documentation / Reference


Powered by ComboStrap