Sobes.tech
Middle+

What happens when you join a closed channel? And what happens when a closed channel is closed?

sobes.tech AI

Answer from AI

Writing to a closed channel in Go causes a panic, as writing to a closed channel is forbidden and results in a runtime error.

Closing an already closed channel also causes a panic because closing a channel more than once is not allowed.

Example:

ch := make(chan int)
close(ch)         // channel is closed
ch <- 1           // panic: send on closed channel
close(ch)         // panic: close of closed channel

Important: reading from a closed channel does not cause a panic; it returns the zero value and false as the second parameter, which allows goroutines to terminate correctly.

What happens when you join a closed channel? And what… - sobes.tech