The preferred approach is to keep a counter with the channel, and atomically increment the counter while writing the channel, and decrementing the counter when reading the channel.
Your solution traverses through all element of the channel, which will probably not work well for actual high-concurrent workloads.