Why does chan not deadlock in goroution?

package main
func main() {
    ch1 := make(chan int)
    go pump(ch1) // pump hangs
}
func pump(ch chan int) {
    ch <- 1
    ch <- 2
}

Why is it that there is nothing wrong with writing a corruption? Blocking the main process will deadlock

Mar.18,2021

deadlocks occur when all processes or threads are waiting for the resource to be released and the resource cannot be released.

here main finishes running without waiting for other goroutine, so no data flows into ch, and a total of main, is executed without blocking, so there is no deadlock. Change the code to:

package main

import "sync"

func main() {
    ch1 := make(chan int)

    var wg sync.WaitGroup
    wg.Add(1)
    go func() {
        ch1 <- 1
        wg.Done()
    }()

    wg.Wait()
    // <-ch1
}

deadlock occurs

Menu