A Tour of Go in Haskell

GitHub Language

Channel is a one-directional tube to transport values. Values are appended to the end of the channel, and removed from the beginning of the channel.

writeChan ch v // Append v to the end of channel ch
readChan ch    // Take the first value from the top of channel ch

Channel instances must be constructed before use.


Different threads can share a channel. If a thread tries to readChan from an empty channel, the thread does not raise an error, but blocks until a value is available. This is used to connect threads into a pipeline.

The example code makes lightweight threads calculate the sum of Int lists. The main thread awaits results from the lightweight threads, then uses them.

stm package provides another implementation of channel. STM will be introduced later. Now keep in mind only that you should use atomically to run stm computations.

< 2 / 11 >