Run time behavior is tested in go1.13.5 in a variety of situations. Pay attention to the code and comments.
package main
func main(a) {
//var ch chan int
//// fatal error: all goroutines are asleep - deadlock!
//ch <- 1
//var ch chan int
//// fatal error: all goroutines are asleep - deadlock!
//<- ch
//var ch chan int
//// panic: close of nil channel
//close(ch)
//ch := make(chan int, 1)
//close(ch)
//// panic: send on closed channel
//ch <- 1
//ch := make(chan int, 1)
//close(ch)
//// immediately return
//<- ch
//ch2 := make(chan int, 1)
//ch2 <- 5
//close(ch2)
//// return one item
//x := <- ch
//log.Printf("x[%+v]", x)
//ch := make(chan int, 1)
//close(ch)
//// panic: close of closed channel
//close(ch)
//ch := make(chan int)
//select {
//case <-ch: // fatal error: all goroutines are asleep - deadlock!
/ /}
}
Copy the code