How-Tos

9 min read


First impressions of Go 1.23's range-over-func feature

If I can knit and crochet, surely I can loop over functions in Go!


Default select case

Select statements … There can be at most one default case and it may appear anywhere in the list of cases. As with a switch statement, a select statement may include either zero or one default case. The behavior of a select statement is changed in important ways when a default case is included. The details are coming soon, but I think this is a good chance to jump ahead a bit and elaborate.


Select statements

Join me again today as I’ll be coding and answering questions on my livestream. Join me Select statements A “select” statement chooses which of a set of possible send or receive operations will proceed. It looks similar to a “switch” statement but with the cases all referring to communication operations. SelectStmt = "select" "{" { CommClause } "}" . CommClause = CommCase ":" StatementList . CommCase = "case" ( SendStmt | RecvStmt ) | "default" .


Go statements, conclusion

Today we finish the description of go statements: Go statements … When the function terminates, its goroutine also terminates. If the function has any return values, they are discarded when the function completes. go Server() go func(ch chan<- bool) { for { sleep(10); ch <- true }} (c) Okay, so that bit about discarding return values makes sense, right? func main() { go sum(1, 3) // return value discarded } func sum(a, b int) int { return a + b } But what if you need that return value for something?


Go statements, continued

Now that we’ve talked about what goroutines are and aren’t, lets look at the details of a Go statement: Go statements … The expression must be a function or method call; it cannot be parenthesized. This is important! The expression must be a functino or method call. Not simply the name of a function or method. That is to say, this: go foo() not that: go foo This may feel pretty natural, but it’s easy (at leat for me) to forget sometimes.


Go statements

At long last, we’re ready to learn about Go’s namesake feature! Go statements A “go” statement starts the execution of a function call as an independent concurrent thread of control, or goroutine, within the same address space. GoStmt = "go" Expression . Now before we talk about the particulars of how a go statement works, I want to set aside a common misconception, that’s subtly addressed in the above description.


What good are nil channels?

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.

Subscribe to Boldly Go: Daily

Every day I'll send you advice to improve your understanding of Go. Don't miss out! I will respect your inbox, and honor my privacy policy.

Unsure? Browse the archive.


Range conclusion

Today we finish up with some final notes, and a big example, from the spec section on for statements. For statements with range clause … If the iteration variables are not explicitly declared by the “range” clause, they must be preexisting. In this case, the iteration values are assigned to the respective variables as in an assignment statement. I actually discussed this case yesterday, so won’t go into it again.


Scope and type of range variables

For statements with range clause … The iteration variables may be declared by the “range” clause using a form of short variable declaration (:=). In this case their scope is the block of the “for” statement and each iteration has its own new variables [Go 1.22] (see also “for” statements with a ForClause). We’ve already talked at some length about the scope of variables in other forms of for loops.


Iteration over integers

I’m back from my holiday! I thought I’d send at least a few dalies over the last week while I was on vacation, but it wasn’t meant to be. But now that I’m back, we’ll pick up where we left off… For statements with range clause … For an integer value n, the iteration values 0 through n-1 are produced in increasing order. If n <= 0, the loop does not run any iterations.


Iteration over channels

Two quick related notes: I missed a day or two last week, due to travel. I’lll be traveling through the middle of next week as well, so these “daily” emails may be slightly less frequent. We’ll see. And related to that, no live stream this week or next. I expect to live stream again July 8. Hope to see you then! For statements with range clause … For channels, the iteration values produced are the successive values sent on the channel until the channel is closed.