تخطَّ إلى المحتوى

Unbuffered Channel Deadlock (ARCH5035)

هذا المحتوى غير متوفر بلغتك بعد.

Key go.async.channel-deadlock · Severity warning · Category async

Sending on an unbuffered channel with no concurrent receiver - or receiving with no sender in the same goroutine - blocks forever. This pattern produces a deadlock that can crash the whole program.

func Compute() int {
ch := make(chan int)
ch <- 42 // no receiver, blocks forever
return <-ch
}
func Compute() int {
ch := make(chan int)
go func() {
ch <- 42 // sent on a separate goroutine
}()
return <-ch
}

Move the send into a separate goroutine or make the channel buffered.