使用iota定义const变量时如何跳过很多值? [英] How to skip a lot of values when define const variable with iota?
问题描述
说我有下一个c程序:
#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);
}
输出为:
0、1、2、99、100
0, 1, 2, 99, 100
如果要去,我如何使用更多golang方式
来处理呢?
If in go, how can I use a more golang way
to handle that?
实际上,如果我只是想跳过一些值.例如打印 0,1,2,5,6
,然后我可以使用next跳过一些值,但是这里我需要跳过96个值...
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)
}
而且,我也可以使用它,但是在 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)
}
那么,我还有什么更多的golang方式
来处理这种情况?
So, is there any more golang way
for me to handle such kind of situation?
推荐答案
单个组,手动偏移量
您可以将 iota
更改为常量,然后将后续的初始化表达式留空:
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
尽管如果在 BANANA
之前插入元素,则 BANANA
的值和后续常量将更改.
Although if you insert elements before BANANA
, values of BANANA
and subsequent constants will change.
如果要避免这种情况,请打破常数组,然后开始新的操作(每当保留字 const时
iota
的值都将重置为 0
出现在源代码中):
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.
如果您不想破坏常数组,那么仍然有解决方案.
If you don't want to break the constant group, there is still a solution.
在要破坏"编号的位置引入一个常数,并在下一行的 iota
中减去其值.这将导致 1
,因此将其移位减去1即可继续.这样,即使您在 BANANA
之前(但不在 _BREAK
和 BANANA
之间插入元素), BANANA
和随后的常量不会改变.
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.
根据您的喜好, _BREAK
可能会使用 iota + 1
进行初始化,因此要应用于下一个常量的偏移量将是它获得的值:
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.
这篇关于使用iota定义const变量时如何跳过很多值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!