What good are nil channels?

July 8, 2024

I’ll be live streaming again today! But this time it’s a bit different.

I found a bug in the Go standard library! 😲

So on today’s live stream, I’ll be coming up with a minimal reproducible case, and filing a bug report. And hopefully also submitting a patch to fix the bug!

Join me


I recently asked you if you knew of any practical uses of a nil channel in Go.

One reader wrote back to point me to a blog post by Francesc Campoy, that offers one possible use, which I think I can extend to a second, realted, possible uses.

Imagine you’re handling multiple channels. The blog post’s example is aggregating two channels into one, but there are many other reasons you may want to handle multiple channels. Here’s a simplified example. Read the blog post for the full explanation. 😊

for chanA != nil && chanB != nil{
	select {
		case value, ok := <- chanA:
			if ok {
				/* do something with value */
				continue
			}
			chanA = nil
		case value, ok := <- chanB:
			if ok {
				/* do something with value */
				continue
			}
			chanB = nil
	}
}

In short, it starts a for loop that runs until both chanA and chanB are nil. Then it reads from both channels, and when it detects that one of the channels has been closed, it sets it to nil, which in turn causes that select case to block forever; always deferring to the other.

I think this example could be simply extended to handle channels that are not yet ready, as well, as a second, related use case. But I’ll leave that implementation to the reader.

Quotes from The Go Programming Language Specification Language version go1.22 (Feb 6, 2024)


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Assignability in Assignments

It hardly needs to be stated that in an assignment, a value must be assignable, but here we are… Assignment statements … In assignments, each value must be assignable to the type of the operand to which it is assigned… We’ve already talked about assignability, so we won’t go into those details here. But there are some special cases to discuss: … with the following special cases: Any typed value may be assigned to the blank identifier.


Wanted: Your Go code to review

In next week’s live stream, I’m going to be reviewing Go code from you, the Boldy Go community. I’ll be looking at three or four projects, which I have never seen before, and offering my feedback. And that’s where you come in. I’d love to review your code! Do you have a Go project on GitHub that you’d like to have reviewed? Maybe it’s a take-home coding assignment you did as part of an interview process, and you’d like some more constructive feedback than you got from the recruiter who ghosted you.


Expressions

There are a number of terms that get thrown around, often semi-interchangeably by the less initiated (such as myself). “Declaration”, “definition”, “statement”, … and today’s topic “expressions”, just to name a few. But, at least within the context of the Go spec, most such terms have very specific meanings. Expressions An expression specifies the computation of a value by applying operators and functions to operands. So: type foo int and var foo int are not an expressions.

Get daily content like this in your inbox!

Subscribe