func main(a) {
var taskChannle =make(chan int)
taskChannle <- 1
fmt.Println(<-taskChannle)
Copy the code
If a channel is initialized without a buffer, it is not cached by default. That is, sending data to a channel is a blocking operation until the Channel reads it. So when the code executes to taskChannle < -1, it blocks and doesn’t move down, so go considers all goroutines are asleep – deadlock! Change the code to the following:
func main(a) {
var taskChannle =make(chan int)
go func(a) {
fmt.Println(<-taskChannle)
}()
taskChannle <- 1
Copy the code
Deadlock is not generated, or buffered channels are declared