我试图代表一个简化的染色体,该染色体由N个碱基组成,每个碱基只能是的一个{A, C, T, G}
。
我想用一个枚举形式化约束,但是我想知道在Go语言中最惯用的枚举方式是什么。
引用语言规范:Iota
在常量声明中,预声明的标识符iota表示连续的无类型整数常量。每当保留字const出现在源中时,它将重置为0,并在每个ConstSpec之后递增。它可以用来构造一组相关的常量:
const ( // iota is reset to 0
c0 = iota // c0 == 0
c1 = iota // c1 == 1
c2 = iota // c2 == 2
)
const (
a = 1 << iota // a == 1 (iota has been reset)
b = 1 << iota // b == 2
c = 1 << iota // c == 4
)
const (
u = iota * 42 // u == 0 (untyped integer constant)
v float64 = iota * 42 // v == 42.0 (float64 constant)
w = iota * 42 // w == 84 (untyped integer constant)
)
const x = iota // x == 0 (iota has been reset)
const y = iota // y == 0 (iota has been reset)
在ExpressionList中,每个iota的值都是相同的,因为它仅在每个ConstSpec之后才增加:
const (
bit0, mask0 = 1 << iota, 1<<iota - 1 // bit0 == 1, mask0 == 0
bit1, mask1 // bit1 == 2, mask1 == 1
_, _ // skips iota == 2
bit3, mask3 // bit3 == 8, mask3 == 7
)
最后一个示例利用了最后一个非空表达式列表的隐式重复。
所以你的代码可能像
const (
A = iota
C
T
G
)
要么
type Base int
const (
A Base = iota
C
T
G
)
如果您希望基数是与int分开的类型。