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 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度