dongtangze6393 2015-03-16 18:00
浏览 141
已采纳

如何在golang中枚举?

I have

const (
  BlahFoo = 1 << iota
  MooFoo
)

then

type Cluster struct {
  a  int
  b  int
}

I want Cluster.a to only be BlahFoo or MooFoo

How do I enforce that?

  • 写回答

1条回答 默认 最新

  • douyu2817 2015-03-16 18:40
    关注
    type FooEnum int
    
    const (
      BlahFoo FooEnum = 1 << iota
      MooFoo
    )
    
    type Cluster struct {
      a FooEnum
      b int
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?