On Fri, Jun 30, 2017 at 10:23 PM, Manohar Reddy <b.manu...@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

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to