我如何在golang进行枚举?

我如何在golang进行枚举?,go,Go,我有 我希望Cluster.a只能是BlahFoo或MooFoo 如何强制执行此操作?键入FooEnum int type Cluster struct { a int b int } 常数( BlahFoo FooEnum=1的可能副本除了通过访问器方法为您检查值之外,没有办法强制执行Go中的值。这可能是您能得到的最好结果。仍然允许您使用Cluster{a:10},但至少会在x:=10;Cluster{a:x}时中断。 type Cluster struct { a int

我有

我希望Cluster.a只能是BlahFoo或MooFoo

如何强制执行此操作?

键入FooEnum int
type Cluster struct {
  a  int
  b  int
}
常数(
BlahFoo FooEnum=1的可能副本除了通过访问器方法为您检查值之外,没有办法强制执行Go中的值。这可能是您能得到的最好结果。仍然允许您使用
Cluster{a:10}
,但至少会在
x:=10;Cluster{a:x}
时中断。
type Cluster struct {
  a  int
  b  int
}
type FooEnum int

const (
  BlahFoo FooEnum = 1 << iota
  MooFoo
)

type Cluster struct {
  a FooEnum
  b int
}