Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do enums in golang?

Tags:

go

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?

like image 417
steve landiss Avatar asked Sep 17 '25 00:09

steve landiss


1 Answers

type FooEnum int

const (
  BlahFoo FooEnum = 1 << iota
  MooFoo
)

type Cluster struct {
  a FooEnum
  b int
}
like image 177
Zikes Avatar answered Sep 19 '25 14:09

Zikes