A channel is a thread-safe queue of values of a given type.

A primary use for channels is to communicate between goroutines.

For that reason we talk about sending values to a channel (ch <- value) and receiving values from a channel (value <- ch).

Basic of channels:

https://codeeval.dev/gist/3b414583e0fd92c4cc3810474b6c1b6d

Zero value of a channel is nil. Sending to nil channel blocks forever so the first thing to do is to create a channel with make(chan <type>, <queue size>). Queue size is optional and defaults to unbuffered channel (you can think of it as size 0).

Send operator chan <- value puts the value at the end of the queue.

If channel is full, <- will block.

Send on a nil channel blocks forever.

Retrieve statement value = <- chan picks up the value from the front of the queue.

If channel is empty, retrieve will block.

Another way to retrieve a value form channel is to use select statement.

Using select allows to:

Yet another is to use range.

Channels have a fixed capacity.

Channel created without explicit size (e.g. make(chan int)) has size 0 and is called unbuffered channel. Send on unbuffered channel blocks until a corresponding receive completes.

Channel created with explicit size (e.g. make(chan int, 3)) is called buffered channel with capacity of 3.