While fixing benchmarks for “two ways to implement orDone”, I found bugs in the binary-recursive version — mainly goroutine leaks. See also learning orDone the hard way.
Goroutine leak
1 | orDone := make(chan interface{}) |
The recursive branches never merged the parent orDone channel. After orDone closed, nested goroutines had no signal to exit.
Fix by appending orDone into each recursive call:
1 | select { |
Infinite recursion
With three channels, the old split still recursed forever. The reference post includes goroutine-count prints if you want to reproduce.
Recursion tree:
1 | // with 3 channels, unbounded recursion: |
Treat n == 3 as its own case.
Final version:
1 | func OrRecur(channels ...<-chan interface{}) <-chan interface{} { |
After the fix, benchmarks favored binary recursion over reflection on time. Memory profiles looked similar.


Benchmark source: Dig101-Go orDone
The orDone pattern looks simple; channels still reward careful shutdown and edge cases.
Questions? Leave a comment below or email: newbvirgil@gmail.com![]()
