We have previously discussed how channels belong to the pointer types in Go, so their default value is nil. But what happens when you send or receive from a nil channel?
If we create a very simple app that tries to send to an empty channel, we get a deadlock:
func main() {
var a chan int
a <- 1
}
The full example is available at https://play.golang.org/p/KHJ4rvxh7TM.
If we do the same for a receiving operation, we get the same result of a deadlock:
func main() {
var a chan int
<-a
}
The full example is available at https://play.golang.org/p/gIjhy7aMxiR.
The last thing left to check is how the close function behaves with a nil channel. It panics with the close of nil channel explicit value:
func main() {
var a chan int
close(a)
}
The full example is available at https://play.golang.org/p/5RjdcYUHLSL.
To recap, we have seen that a nil channel's send and receive are blocking operations, and that close causes a panic.