Think of a bounded variable no different than a bounded queue to solve the consumer—producer problem.
A very common use case is for me is to push to a single variable from one producer, and pull from that single variable from one consumer.
Go has a very simple solution: channels.
But I was curious: can we improve the performance?
Turns out, you can't.
Channels are the winner.
goos: darwin
goarch: arm64
pkg: boundedbuffer
BenchmarkBoundedVariable-8 4586 251930 ns/op 249 B/op 5 allocs/op
BenchmarkChannel-8 9801 121622 ns/op 144 B/op 2 allocs/op
PASS
boundedbuffer coverage: 92.3% of statements
ok boundedbuffer 3.252s