Go Enumerations: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 9: Line 9:
=Overview=
=Overview=


Go does not have formal enums, but the language allows for sets of related, yet distinct <code>int</code> constants. They represent a property that has several distinct possible <code>int</code> values, like the days of the weeks or the months of the year. They are declared using the [[Go_Language#Pre-Declared_Constants|pre-declared constant]] <code>[[Go_iota|iota]]</code>:
Go does not have formal enums, but the language allows for sets of related, yet distinct <code>int</code> constants. They represent a property that has several distinct possible <code>int</code> values, like the days of the weeks or the months of the year.  
 
 
These auto-incrementing <code>int</code> constants are declared using a special syntax involving the <code>const</code> keyword, parentheses and the [[Go_Language#Pre-Declared_Constants|pre-declared constant]] <code>[[Go_iota|iota]]</code>:
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
type DayOfTheWeek int
type DayOfTheWeek int
Line 22: Line 25:
)
)
</syntaxhighlight>
</syntaxhighlight>
Only the first constant in the series declares its type and an expression involving <code>iota</code>. The simplest is <code>iota</code> itself, but more complex expressions can be used. During the initialization, the compiler advances through the list of constants, and for each advancement, <code>iota</code> is incremented with 1. It starts with <code>0</code>.


To make the enumeration constants visible outside the package, they need to start with a capital letter.
To make the enumeration constants visible outside the package, they need to start with a capital letter.

Revision as of 23:03, 12 September 2024

External

Internal

Overview

Go does not have formal enums, but the language allows for sets of related, yet distinct int constants. They represent a property that has several distinct possible int values, like the days of the weeks or the months of the year.


These auto-incrementing int constants are declared using a special syntax involving the const keyword, parentheses and the pre-declared constant iota:

type DayOfTheWeek int
const (
  MON DayOfTheWeek = iota
  TUE
  WED
  THU
  FRI
  SAT
  SUN
)

Only the first constant in the series declares its type and an expression involving iota. The simplest is iota itself, but more complex expressions can be used. During the initialization, the compiler advances through the list of constants, and for each advancement, iota is incremented with 1. It starts with 0.

To make the enumeration constants visible outside the package, they need to start with a capital letter.

Conversion to and from string representations:

type DayOfTheWeek int

const (
	MON DayOfTheWeek = iota
	TUE
	WED
	THU
	FRI
	SAT
	SUN
)

var dayOfTheWeekToString = []string{
	"Monday",
	"Tuesday",
	"Wednesday",
	"Thursday",
	"Friday",
	"Saturday",
	"Sunday",
}

func (s DayOfTheWeek) String() string {
	return dayOfTheWeekToString[s]
}

func StringToDayOfTheWeek(s string) DayOfTheWeek {
	for i, v := range dayOfTheWeekToString {
		if s == v {
			return DayOfTheWeek(i)
		}
	}
	return DayOfTheWeek(-1)
}