Start Enums at One

The standard way of introducing enumerations in Go is to declare a custom typeand a const group with iota. Since variables have a 0 default value, youshould usually start your enums on a non-zero value.

BadGood
  1. type Operation int
  2.  
  3. const (
  4. Add Operation = iota
  5. Subtract
  6. Multiply
  7. )
  8.  
  9. // Add=0, Subtract=1, Multiply=2
  1. type Operation int
  2.  
  3. const (
  4. Add Operation = iota + 1
  5. Subtract
  6. Multiply
  7. )
  8.  
  9. // Add=1, Subtract=2, Multiply=3

There are cases where using the zero value makes sense, for example when thezero value case is the desirable default behavior.

  1. type LogOutput int
  2.  
  3. const (
  4. LogToStdout LogOutput = iota
  5. LogToFile
  6. LogToRemote
  7. )
  8.  
  9. // LogToStdout=0, LogToFile=1, LogToRemote=2