douqixia7942
2019-07-16 08:42
浏览 136
已采纳

使用iota定义const变量时如何跳过很多值?

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
", 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?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongqixuan3112 2019-07-16 08:46
    已采纳

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题