goenumsconstantsiota

How to skip a lot of values when define const variable with iota?


Say I have next c program:

#include <stdio.h>

int main(int args, char* argv[])
{
    enum RC {
        APPLE=0,
        ORANGE,
        PEAR,
        BANANA=99,
        GRAPE
    };

    printf("%d, %d, %d, %d, %d\n", APPLE, ORANGE, PEAR, BANANA, GRAPE);
}

The output is:

0, 1, 2, 99, 100

If in go, how can I use a more golang way to handle that?

In fact, if I just want to skip some value. e.g. print 0, 1, 2, 5, 6, then I can use next to skip some value, but here I need to skip 96 values...

package main

import "fmt"

func main() {
    const (
        APPLE = iota
        ORANGE
        PEAR
        _
        _
        BANANA
        GRAPE
    )
    fmt.Println(APPLE, ORANGE, PEAR, BANANA, GRAPE)
}

And, also I can use next, but I still have many const variable after GRAPE...

package main

import "fmt"

func main() {
    const (
        APPLE = iota
        ORANGE
        PEAR
        BANANA = 99
        GRAPE  = 100
    )
    fmt.Println(APPLE, ORANGE, PEAR, BANANA, GRAPE)
}

So, is there any more golang way for me to handle such kind of situation?


Solution

  • Single group, manual offset

    You may shift the iota with a constant, and leave subsequent initialization expressions empty:

    const (
        APPLE = iota
        ORANGE
        PEAR
        BANANA = iota + 96 // 96 is manually calculated to get 99
        GRAPE
    )
    fmt.Println(APPLE, ORANGE, PEAR, BANANA, GRAPE)
    

    This will output (try it on the Go Playground):

    0 1 2 99 100
    

    Although if you insert elements before BANANA, values of BANANA and subsequent constants will change.

    Breaking the constant group

    If you want to avoid that, break the constant group, and start a new (the value of iota is reset to 0 whenever the reserved word const appears in the source):

    const (
        APPLE = iota
        ORANGE
        PEAR
    )
    const (
        BANANA = iota + 99 // iota is reset to 0
        GRAPE
    )
    fmt.Println(APPLE, ORANGE, PEAR, BANANA, GRAPE)
    

    Output is the same. Try this one on the Go Playground.

    Single group, automatic offset

    If you don't want to break the constant group, there is still a solution.

    Introduce a constant in the place where you want to "break" the numbering, and subtract its value from iota in the the subsequent line. This will result in 1, and so shift this with the amount minus 1 you want to continue from. This way even if you insert elements before BANANA (but not between _BREAK and BANANA), ordinals of BANANA and subsequent constants won't change.

    const (
        APPLE = iota
        ORANGE
        PEAR
    
        _BREAK
    
        BANANA = iota - _BREAK + 98 // Continues from 98 + 1 = 99
        GRAPE
    )
    fmt.Println(APPLE, ORANGE, PEAR, BANANA, GRAPE)
    

    Output again is the same. Try it on the Go Playground.

    Depending on your taste, _BREAK may be initialized with iota + 1, so the offset to apply on the next constant will be the value it gets:

    const (
        APPLE = iota
        ORANGE
        PEAR
    
        _BREAK = iota + 1
    
        BANANA = iota - _BREAK + 99 // Continues from 99
        GRAPE
    )
    

    Try this one on the Go Playground. Use whichever looks easier to you.