Go Enumerations: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * Go Language =Overview= Go does not have formal enums, but the language allows for sets of related, yet distinct constants. They represent a property that has several distinct possible values, like the days of the weeks or the months of the year. They are declared using the pre-declared constant <code>iota</code>: <syntaxhighlight lang='go'> type DayOfTheWeek int const ( MON D...")
 
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* https://blog.boot.dev/golang/golang-enum
=Internal=
=Internal=
* [[Go_Language#Enumerations|Go Language]]
* [[Go_Language#Enumerations|Go Language]]
Line 4: Line 7:
=Overview=
=Overview=


Go does not have formal enums, but the language allows for sets of related, yet distinct constants. They represent a property that has several distinct possible 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. They are declared using 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 16: Line 19:
   SUN
   SUN
)
)
</syntaxhighlight>
To make the enumeration constants visible outside the package, they need to start with a capital letter.
Conversion to and from string representations:
<syntaxhighlight lang='go'>
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)
}
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 23:27, 11 March 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. They are declared using the pre-declared constant iota:

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

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)
}