在 Go 中表示枚举的惯用方式是什么? [英] What is an idiomatic way of representing enums in Go?

查看:37
本文介绍了在 Go 中表示枚举的惯用方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图表示一个简化的染色体,它由 N 个碱基组成,每个碱基只能是 {A, C, T, G} 之一.

我想用枚举形式化约束,但我想知道在 Go 中模拟枚举最惯用的方法是什么.

解决方案

引用语言规范:Iota><块引用>

在常量声明中,预先声明的标识符 iota 表示连续的无类型整数常量.每当保留字 const 出现在源中并在每个 ConstSpec 之后递增时,它就会重置为 0.可以用来构造一组相关的常量:

const (//iota 被重置为 0c0 = iota//c0 == 0c1 = iota//c1 == 1c2 = iota//c2 == 2)常量 (a = 1<<iota//a == 1(iota 已重置)b = 1<<iota//b == 2c=1<<iota//c == 4)常量 (u = iota * 42//u == 0(无类型整数常量)v float64 = iota * 42//v == 42.0(float64 常量)w = iota * 42//w == 84(无类型整数常量))const x = iota//x == 0(iota 已被重置)const y = iota//y == 0(iota 已被重置)

<块引用>

在 ExpressionList 中,每个 iota 的值都是相同的,因为它只在每个 ConstSpec 之后递增:

const (位 0,掩码 0 = 1 <<iota, 1<<iota - 1//bit0 == 1, mask0 == 0位 1,掩码 1//位 1 == 2,掩码 1 == 1_, _//跳过 iota == 2bit3, mask3//bit3 == 8, mask3 == 7)

<块引用>

最后一个例子利用了最后一个非空表达式列表的隐式重复.

<小时>

所以你的代码可能像

const (A = iotaC吨G)

type Base int常量 (基数 = iotaC吨G)

如果你想让 bases 是一个独立于 int 的类型.

I'm trying to represent a simplified chromosome, which consists of N bases, each of which can only be one of {A, C, T, G}.

I'd like to formalize the constraints with an enum, but I'm wondering what the most idiomatic way of emulating an enum is in Go.

解决方案

Quoting from the language specs:Iota

Within a constant declaration, the predeclared identifier iota represents successive untyped integer constants. It is reset to 0 whenever the reserved word const appears in the source and increments after each ConstSpec. It can be used to construct a set of related constants:

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)

Within an ExpressionList, the value of each iota is the same because it is only incremented after each 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
)

This last example exploits the implicit repetition of the last non-empty expression list.


So your code might be like

const (
        A = iota
        C
        T
        G
)

or

type Base int

const (
        A Base = iota
        C
        T
        G
)

if you want bases to be a separate type from int.

这篇关于在 Go 中表示枚举的惯用方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆