Few notes from this article and exploration.
Sending through channel is synchronous. This means it blocks if channel is full.
A common pattern: making a channel and returning it; instead of returning value returning the channel and pushing the value into the channel.
| |
To close a channel: defer close(channel); A channel can only be closed once. GC will collect a channel regardless of it is closed or not (if it is not referenced).
Tri-color marking
GO uses something called a tracing GC.
It is a classic mark and sweep.
- concurrently run and mark every object in the heap into white(garbage)
- gray: discovered these but not whats inside them
- black: disconvered these and all the objects they point to are at least gray
The processing starts from the roots(the variables on the stack and globals) and marks stuff pointed by it (classic mark and sweep).
Because this is concurrent, the compiler does some magic (white barrier; injected code) to prevent GC from deleting a new object (concurrently created). It marks the new object Gray.
Green Tea
The fancy new thing in 1.26 that was praised.
Because the mark-and-sweep described above process is just pointer chasing, cache locality was all over the place. CPU was free but data wasn’t available (cache misses). Green Tea solves this. Go’s alloator groups objects of similar size together(spans). Spans can be multiple memory pages. Instead of just loading what the pointer points to it loads a entire span and more often than not it hits cache.
GOMEMLIMIT tell go in runtime how much memory is allowed. To prevent frequent GC runs.
range can iterate over a channel.
Channel can have directions.

nil channel: writing or reading to a nil channel blocks forever. Close nil channel = panic
| |
Building pipelines using channels:
| |
Setting in1 = nil means next iteration will pull from nil which blocks! Otherwise, pulling from a closed channel will return default value 0, false and it will spin forever.