我正在尝试表示一个简化的染色体,其中包含N个碱基,每个碱基只能是{A,C,T,G}
中的一个。
我想用枚举来规范约束条件,但我想知道在Go语言中模拟枚举最惯用的方式是什么。
我正在尝试表示一个简化的染色体,其中包含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)
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
)
iota + 1
来避免从0开始计数。 - Marçal JuanA base = iota
的方式,所有的值都正确地具有了类型Base
。如果它们是未命名的数字常量,我们就可以将它们用作整数,但现在我们不能这样做。代码示例:https://play.golang.org/p/Dpop4S3qdNs - Deleplace参考jnml的回答,您可以通过根本不导出Base类型(即将其写成小写)来防止新实例化。如果需要,您可以创建一个可导出的接口,该接口具有返回基础类型的方法。此接口可在外部处理Bases的函数中使用,例如:
package a
type base int
const (
A base = iota
C
T
G
)
type Baser interface {
Base() base
}
// every base must fulfill the Baser interface
func(b base) Base() base {
return b
}
func(b base) OtherMethod() {
}
package main
import "a"
// func from the outside that handles a.base via a.Baser
// since a.base is not exported, only exported bases that are created within package a may be used, like a.A, a.C, a.T. and a.G
func HandleBasers(b a.Baser) {
base := b.Base()
base.OtherMethod()
}
// func from the outside that returns a.A or a.C, depending of condition
func AorC(condition bool) a.Baser {
if condition {
return a.A
}
return a.C
}
在主包中,a.Baser
现在实际上类似于一个枚举类型。只有在 a 包内才能定义新的实例。
base
仅用作方法接收器的情况。如果你的a
包公开一个以base
类型参数为输入的函数,那就会变得危险。因为用户可以使用字面值42直接调用该函数,而该函数将接受它作为base
类型,因为它可以转换为int。为了防止这种情况,让base
成为一个结构体:type base struct{value:int}
。但是问题是,你不能再声明base
为常量,只能声明为模块变量。但是42永远不会被转换为该类型的base
。 - Niriel您可以这样做:
type MessageType int32
const (
TEXT MessageType = 0
BINARY MessageType = 1
)
使用此代码,编译器应检查枚举类型
确实,使用const
和iota
的上述示例是在Go中表示基本枚举的最传统方式。但如果您正在寻找一种创建更完整功能的枚举类型(类似于Java或Python中看到的类型)的方法呢?
创建一个对象,使其开始看起来和感觉像Python中的字符串枚举类型非常简单:
package main
import (
"fmt"
)
var Colors = newColorRegistry()
func newColorRegistry() *colorRegistry {
return &colorRegistry{
Red: "red",
Green: "green",
Blue: "blue",
}
}
type colorRegistry struct {
Red string
Green string
Blue string
}
func main() {
fmt.Println(Colors.Red)
}
假设您还需要一些实用方法,例如Colors.List()
和Colors.Parse("red")
。而且您的颜色更加复杂,需要作为结构体。那么您可以按照以下方式进行操作:
package main
import (
"errors"
"fmt"
)
var Colors = newColorRegistry()
type Color struct {
StringRepresentation string
Hex string
}
func (c *Color) String() string {
return c.StringRepresentation
}
func newColorRegistry() *colorRegistry {
red := &Color{"red", "F00"}
green := &Color{"green", "0F0"}
blue := &Color{"blue", "00F"}
return &colorRegistry{
Red: red,
Green: green,
Blue: blue,
colors: []*Color{red, green, blue},
}
}
type colorRegistry struct {
Red *Color
Green *Color
Blue *Color
colors []*Color
}
func (c *colorRegistry) List() []*Color {
return c.colors
}
func (c *colorRegistry) Parse(s string) (*Color, error) {
for _, color := range c.List() {
if color.String() == s {
return color, nil
}
}
return nil, errors.New("couldn't find it")
}
func main() {
fmt.Printf("%s\n", Colors.List())
}
在这一点上,当然它可以工作,但您可能不喜欢必须反复定义颜色的方式。如果您想消除这种情况,可以在结构体上使用标签并进行一些花哨的反射来设置它,但希望这足以涵盖大多数人。
case Colors.Red: ...
。 - David Good使用结构体命名空间可以解决这个问题。
好处是所有枚举变量都在特定的命名空间中,以避免污染。
问题是我们只能使用var
而不是const
type OrderStatusType string
var OrderStatus = struct {
APPROVED OrderStatusType
APPROVAL_PENDING OrderStatusType
REJECTED OrderStatusType
REVISION_PENDING OrderStatusType
}{
APPROVED: "approved",
APPROVAL_PENDING: "approval pending",
REJECTED: "rejected",
REVISION_PENDING: "revision pending",
}
go generate
工具与stringer
命令一起推出,使您的枚举类型易于调试和打印。对于这样的用例,使用字符串常量可能很有用,因为它可以转换为JSON字符串。在下面的示例中,[]Base{A,C,G,T}
将被转换为["adenine","cytosine","guanine","thymine"]
。
type Base string
const (
A Base = "adenine"
C = "cytosine"
G = "guanine"
T = "thymine"
)
使用 iota
时,值会被编组成整数。在以下示例中,[]Base{A,C,G,T}
会被编组为 [0,1,2,3]
。
type Base int
const (
A Base = iota
C
G
T
)
以下是一个比较两种方法的示例:
我相信这里有很多好的答案。但是,我想分享一下我使用枚举类型的方法。
package main
import "fmt"
type Enum interface {
name() string
ordinal() int
values() *[]string
}
type GenderType uint
const (
MALE = iota
FEMALE
)
var genderTypeStrings = []string{
"MALE",
"FEMALE",
}
func (gt GenderType) name() string {
return genderTypeStrings[gt]
}
func (gt GenderType) ordinal() int {
return int(gt)
}
func (gt GenderType) values() *[]string {
return &genderTypeStrings
}
func main() {
var ds GenderType = MALE
fmt.Printf("The Gender is %s\n", ds.name())
}
这绝对是我们可以在Go中创建枚举类型并使用的惯用方式之一。
编辑:
添加了另一种使用常量进行枚举的方法
package main
import (
"fmt"
)
const (
// UNSPECIFIED logs nothing
UNSPECIFIED Level = iota // 0 :
// TRACE logs everything
TRACE // 1
// INFO logs Info, Warnings and Errors
INFO // 2
// WARNING logs Warning and Errors
WARNING // 3
// ERROR just logs Errors
ERROR // 4
)
// Level holds the log level.
type Level int
func SetLogLevel(level Level) {
switch level {
case TRACE:
fmt.Println("trace")
return
case INFO:
fmt.Println("info")
return
case WARNING:
fmt.Println("warning")
return
case ERROR:
fmt.Println("error")
return
default:
fmt.Println("default")
return
}
}
func main() {
SetLogLevel(INFO)
}
enum.Name(index int)
:返回给定索引的名称。enum.Index(name string)
:返回给定名称的索引。enum.Last()
:返回最后一个枚举的索引和名称type EnumItem struct {
index int
name string
}
type Enum struct {
items []EnumItem
}
func (enum Enum) Name(findIndex int) string {
for _, item := range enum.items {
if item.index == findIndex {
return item.name
}
}
return "ID not found"
}
func (enum Enum) Index(findName string) int {
for idx, item := range enum.items {
if findName == item.name {
return idx
}
}
return -1
}
func (enum Enum) Last() (int, string) {
n := len(enum.items)
return n - 1, enum.items[n-1].name
}
var AgentTypes = Enum{[]EnumItem{{0, "StaffMember"}, {1, "Organization"}, {1, "Automated"}}}
var AccountTypes = Enum{[]EnumItem{{0, "Basic"}, {1, "Advanced"}}}
var FlagTypes = Enum{[]EnumItem{{0, "Custom"}, {1, "System"}}}
重构了https://dev59.com/aWYq5IYBdhLWcg3wfw1x#17989915以使其更易读:
package SampleEnum
type EFoo int
const (
A EFoo = iota
C
T
G
)
type IEFoo interface {
Get() EFoo
}
func(e EFoo) Get() EFoo { // every EFoo must fulfill the IEFoo interface
return e
}
func(e EFoo) otherMethod() { // "private"
//some logic
}