nil channel VS closed channel

The zero value of channel type is nil, and the send and receive operations on a nil channel will always block. Check the following example: package main import "fmt" func main() { var ch chan int go func(c chan int) { for v := range c { fmt.Println(v) } }(ch) ch <- 1 } The running result is like this: fatal error: all goroutines are asleep - deadlock! goroutine 1 [chan send (nil chan)]: main. [Read More]

"nil slice" vs "nil map"

Slice and map are all reference types in Go, and their default values are nil: package main import "fmt" func main() { var ( s []int m map[int]bool ) if s == nil { fmt.Println("The value of s is nil") } if m == nil { fmt.Println("The value of m is nil") } } The result is like this: The value of s is nil The value of m is nil When a slice’s value is nil, you could also do operations on it, such as append: [Read More]