4

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?

steve landiss
  • 1,833
  • 3
  • 19
  • 30
  • 5
    possible duplicate of [What is an idiomatic way of representing enums in golang?](http://stackoverflow.com/questions/14426366/what-is-an-idiomatic-way-of-representing-enums-in-golang) – JimB Mar 16 '15 at 18:12
  • There's no way to enforce the value in Go, except via accessor methods to check the value for you. – JimB Mar 16 '15 at 18:18

1 Answers1

9
type FooEnum int

const (
  BlahFoo FooEnum = 1 << iota
  MooFoo
)

type Cluster struct {
  a FooEnum
  b int
}
Zikes
  • 5,888
  • 1
  • 30
  • 44
  • 1
    This is probably best you can get. Will still allow you for `Cluster{a: 10}`, but at least would break on `x := 10; Cluster{a: x}`. – tomasz Mar 16 '15 at 18:46