Iota, continued

July 17, 2023

No live stream today. My office/recording studio is being painted. I’ll be back next week with more linter building!


One last detail on iota

Iota

By definition, multiple uses of iota in the same ConstSpec all have the same value:

const (
	bit0, mask0 = 1 << iota, 1<<iota - 1  // bit0 == 1, mask0 == 0  (iota == 0)
	bit1, mask1                           // bit1 == 2, mask1 == 1  (iota == 1)
	_, _                                  //                        (iota == 2, unused)
	bit3, mask3                           // bit3 == 8, mask3 == 7  (iota == 3)
)

This last example exploits the [implicit repetition](https://go.dev/ref/spec#Constant_declarations) of the last non-empty expression list.

In other words, if you include multiple constant indentifiers within a single ConstantSpec (that is, on a single line), iota has the same value for the entire line.

	const (
		a = iota
		b
		c, d = iota, iota
		e, f
	)
	fmt.Println(a, b, c, d, e, f)

Prints: 0 1 2 2 3 3

I’ve never used this capability. And in fact, until reading this portion of the spec today, I didn’t know it was possible. I’d suggest it’s a feature not likely to be used by many people. In fact, iota itself is such a feature. It’s rare that I use it, and would encourage most people to explicitly declare all constants, rather than relying on it, in the majority of cases.

Quotes from The Go Programming Language Specification Version of December 15, 2022


Share this

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

Unsure? Browse the archive .

Related Content


Iota

You may recall from yesterday that you can omit the constant expression for all but the first ConstantSpec in a parenthesized constant declaration, but that it wasn’t very useful by itself. Enter iota… Iota Within a constant declaration, the predeclared identifier iota represents successive untyped integer constants. Its value is the index of the respective ConstSpec in that constant declaration, starting at zero. It can be used to construct a set of related constants:


Constant errors

Last week I talked about how to create your own errors. Expanding on that idea, here’s a trick I like to use: Constant errors! Constants in Go are limited to simple underlying types: bool, string, and the numeric types. If we create our own type, which both satisfies the error interface, and has such an underlying type, we can then create constant error values: type errString string func (e errString) Error() string { return string(e) } const ErrTooManyFrogs = errString("too many frogs") Okay.


Constant lengths and expressions

A few of the built-in functions are very special, in that they can evaluate to constant expressions. len and cap are two such functions. But they aren’t always evaluated to constant expressions, sometimes they’re more normal-ish runtime functions. Length and capacity … The expression len(s) is constant if s is a string constant. The expressions len(s) and cap(s) are constants if the type of s is an array or pointer to an array and the expression s does not contain channel receives or (non-constant) function calls; in this case s is not evaluated.

Get daily content like this in your inbox!

Subscribe