在Go中创建一个由一组相关常量组成的枚举。

huangapple go评论73阅读模式
英文:

Create an enum from a group of related constants in Go

问题

在Go语言中,将大量相关的常量分组的首选(或正确)方式是什么?例如,C#和C++都有enum来实现这一点。

英文:

What is preferred (or right) way to group large number of related constants in the Go language? For example C# and C++ both have enum for this.

答案1

得分: 16

const?

const (
    pi = 3.14
    foo = 42
    bar = "hello"
)
英文:

const?

const (
    pi = 3.14
    foo = 42
    bar = "hello"
)

答案2

得分: 14

有两种选择,取决于常量将如何使用。

第一种是基于int创建一个新类型,并使用这个新类型声明常量,例如:

type MyFlag int

const (
    Foo MyFlag = 1
    Bar
)

FooBar将具有类型MyFlag。如果你想从MyFlag中提取回int值,你需要进行类型转换:

var i int = int( Bar )

如果这样不方便,可以使用newacct的建议,使用一个裸的const块:

const (
    Foo = 1
    Bar = 2
)

FooBar是完美的常量,可以赋值给int、float等。

这在Effective Go的常量部分有介绍。还可以参考关于iota关键字的讨论,用于自动分配类似于C/C++的值。

英文:

There are two options, depending on how the constants will be used.

The first is to create a new type based on int, and declare your constants using this new type, e.g.:

type MyFlag int

const (
    Foo MyFlag = 1
    Bar
)

Foo and Bar will have type MyFlag. If you want to extract the int value back from a MyFlag, you need a type coersion:

var i int = int( Bar )

If this is inconvenient, use newacct's suggestion of a bare const block:

const (
    Foo = 1
    Bar = 2
)

Foo and Bar are perfect constants that can be assigned to int, float, etc.

This is covered in Effective Go in the Constants section. See also the discussion of the iota keyword for auto-assignment of values like C/C++.

答案3

得分: 11

我对枚举的最接近方法是将常量声明为一种类型。至少你有一些类型安全性,这是枚举类型的主要优点。

type PoiType string

const (
	Camping            PoiType = "Camping"
	Eatery             PoiType = "Eatery"
    Viewpoint          PoiType = "Viewpoint"
)
英文:

My closest approach to enums is to declare constants as a type. At least you have some type-safety which is the major perk of an enum type.

type PoiType string

const (
	Camping            PoiType = "Camping"
	Eatery             PoiType = "Eatery"
    Viewpoint          PoiType = "Viewpoint"
)

答案4

得分: 3

这取决于你想通过这种分组实现什么。Go语言允许使用以下大括号语法进行分组:

const (
    c0 = 123
    c1 = 67.23
    c2 = "string"
)

这只是为程序员添加了一个漂亮的可视块(编辑器可以折叠它),但对编译器没有任何影响(你不能为块指定名称)。

唯一依赖于这个块的是Go中的iota常量声明(对于枚举非常方便)。它允许你轻松地创建自增(不仅仅是自增:在链接中有更多信息)。

例如,这个:

const (
	c0 = 3 + 5 * iota
	c1
	c2
)

将创建常量 c0 = 3(3 + 5 * 0),c1 = 8(3 + 5 * 1)和 c2 = 13(3 + 5 * 2)。

英文:

It depends on what do you want to achieve by this grouping. Go allows grouping with the following braces syntax:

const (
    c0 = 123
    c1 = 67.23
    c2 = "string"
)

Which just adds nice visual block for a programmer (editors allow to fold it), but does nothing for a compiler (you can not specify a name for a block).

The only thing that depends on this block is the iota constant declaration in Go (which is pretty handy for enums). It allows you to create auto increments easily (way more than just auto increments: more on this in the link).

For example this:

const (
	c0 = 3 + 5 * iota
	c1
	c2
)

will create constants c0 = 3 (3 + 5 * 0), c1 = 8 (3 + 5 * 1) and c2 = 13 (3 + 5 * 2).

huangapple
  • 本文由 发表于 2011年8月4日 15:31:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/6937716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定