Explain the meaning of iota

542 views
Skip to first unread message

Manohar Reddy

unread,
Jun 30, 2017, 11:28:50 PM6/30/17
to golang-nuts
`iota` is golnag's enum. I've seen this code in Wikipedia. But I did not understand it. Can someone please explain this code?

type ByteSize float64

const (
    _           = iota // ignore first value by assigning to blank identifier
    KB ByteSize = 1 << (10 * iota)
    MB
    GB
)

Shawn Milochik

unread,
Jun 30, 2017, 11:47:26 PM6/30/17
to golang-nuts

Scott Pakin

unread,
Jul 1, 2017, 7:36:02 PM7/1/17
to golang-nuts
On Friday, June 30, 2017 at 9:28:50 PM UTC-6, Manohar Reddy wrote:
`iota` is golnag's enum. I've seen this code in Wikipedia. But I did not understand it. Can someone please explain this code?

In the first line of the const block, iota is 0, and this is assigned to "_" (i.e., discarded).

In the second line of the const block, iota is 1, so KB (of type ByteSize) is defined as 1 << (10 * 1), which is 1024.

The third line of the const block inherits the previous line's pattern so it's effectively KB ByteSize = 1 << (10 * iota). Because iota is now 2, MB is defined as 1 << (10 * 2), which is 1048576.

Likewise, in the fourth line of the const block, iota is 3 so GB is defined as 1 << (10 * 3), which is 1073741824.

— Scott

Sam Whited

unread,
Jul 2, 2017, 5:35:40 PM7/2/17
to Manohar Reddy, golang-nuts
On Fri, Jun 30, 2017 at 10:23 PM, Manohar Reddy <b.ma...@gmail.com> wrote:
> `iota` is golnag's enum. I've seen this code in Wikipedia. But I did not
> understand it. Can someone please explain this code?

This is slightly nitpicky since many languages don't use enums for
much more than what iota does, but iota actually has nothing to do
with enumeration types, nor is it related to enums in any way (if you
want to know more about enums, which Go doesn't have, there are plenty
of resources online).

Iota is a part of many languages (I think it was originally from APL,
but there may be older usages that I'm not aware of) and is simply a
way to create a list of increasing integers. Three things are
happening in the code snippet you've pasted:

1. the blank identifier (underscore) is used to ignore a value
(https://golang.org/ref/spec#Blank_identifier)
2. In const lists, the expression list is omitted in so it's the same
as the expression from the previous const
(https://golang.org/ref/spec#Constant_declarations)
3. Iota is being used and is incremented for each const assignment;
it's 0 in the first one, 1 in the second, etc.
(https://golang.org/ref/spec#Iota)

This means that the code you pasted is the same as if you'd written:

type ByteSize float64

const (
_ = 0 // ignore first value by assigning to blank identifier
KB ByteSize = 1 << (10 * 1)
MB = 1 << (10 * 2)
GB = 1 << (10 * 3)
)

—Sam

Sam Whited

unread,
Jul 2, 2017, 5:37:51 PM7/2/17
to Manohar Reddy, golang-nuts
On Sun, Jul 2, 2017 at 4:34 PM, Sam Whited <s...@samwhited.com> wrote:
> This means that the code you pasted is the same as if you'd written:
>
> type ByteSize float64
>
> const (
> _ = 0 // ignore first value by assigning to blank identifier
> KB ByteSize = 1 << (10 * 1)
> MB = 1 << (10 * 2)
> GB = 1 << (10 * 3)
> )

*sigh* I really can't send an email to this list without finding a
mistake as soon as I've sent it despite having checked half a dozen
times. That should be:

> const (
> _ = 0 // ignore first value by assigning to blank identifier
> KB ByteSize = 1 << (10 * 1)
> MB ByteSize = 1 << (10 * 2)
> GB ByteSize = 1 << (10 * 3)
> )

—Sam

Bartosz Grzybowski

unread,
Jul 3, 2017, 10:45:52 PM7/3/17
to golang-nuts, b.ma...@gmail.com
Was looking for this for some time, great explanation, thanks !
Reply all
Reply to author
Forward
0 new messages