I am wondering as to why buffered channels of length zero exist in the language. They seem to just yield deadlock in my experience. Is there any use for the at all?
From this answer (emphasis mine):
By default, a channel has a buffer size of 0 (you get this with
make(chan int)
). This means that every single send will block until another goroutine receives from the channel.
If you’re receiving deadlocks, then it’s likely that either nothing is reading from the channel, or something is writing faster than you can read.
A channel of size zero is unbuffered. This means that every single send will block until another goroutine receives from the channel. This example will work:
package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan int, 0)
go func() {
for c := range ch {
fmt.Println(c)
}
}()
ch <- 1
ch <- 2
time.Sleep(time.Minute)
}