Golang & Concurrency (50 Qs)•Part 2: Concurrency, Channels & The GMP Scheduler (16 - 32)
Rules of Channel Closing: Why should only the sender close, and what happens when reading from a closed channel?
senior LevelGolang
In Simple Words:
- Closing an already closed channel triggers a fatal runtime panic.
- Sending to a closed channel triggers a fatal runtime panic.
- Reading from a closed channel NEVER blocks: it returns the zero-value of the type and
false(val, ok := <-ch). - Golden Rule: Always close a channel from the Sender side, never the receiver side, and never when multiple concurrent senders exist.