A goroutine is a lightweight thread managed by the Go runtime.
Channels
Channels are a typed conduit through whichyou can send and receive
values with the channel operator `←`. By default, sends and receives
block until the other side is ready. This allows goroutines to
synchronize without explicit locks or condition variables. Channels
should generally be used for passing ownership of
data.
Channels can also be buffered. Provide the buffer length as the
second argument to `make` to initialize a buffered channel:
Sends to a buffered channel block only when the buffer is full. Receives
block when the buffer is empty.
Range and close
A sender can close a channel to indicate that no more values will be
sent. Receivers can test whether a channel has been closed by assigning
a second parameter to the receive expression.
Only the sender should close a channel, never the receiver. Sending on a
closed channel will cause a panic. Channels aren’t like files; you don’t
usually need to close them. Closing is only necessary when the receiver
must be told there are no more values coming, such as to terminate a
range loop.
Select
The select statement lets a goroutine wait on multiple communication
operations.
A select blocks until one of its cases can run, then it executes that
case. It chooses one at random if multiple are ready.
`default` case in `select` is run if no other case is ready, as one
would expect
Timeout
Often you want to set a timeout value for select so it won’t run
forver. time.After is a good way
of doing this:
sync.Mutex
TO make sure only one goroutine at a time can access a variable we can
use `sync.Mutex`