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

使用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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。