#Is it possible to iterate over all enum values in GoLang?
13 messages · Page 1 of 1 (latest)
go doesn't have enums
there is iota but it's just a cleaver constant trick so no
What should I do to iterate over three types of things that share the same logic?
I don't understand your question, can you show an example ?
You don't iterate over types (unless you are using reflect but you don't want to do that)
Basically I am using OnThisDay api from Wikipedia and there are three types of events: News, Births And Deaths
Now I want to do a nested loop, first iterate through the three types and then iterate through all of the days of the year and fetch all types of events
but like thoses are implemented as three different structs ?
No
type IncidentType rune
const (
Event IncidentType = iota
Birth
Death
)
type Incident struct {
Summary string
IncidentType IncidentType
IncidentInDetail string
Year int
}
then you just do:
for _, e := range [...]IncidentType{
Event,
Birth,
Death,
}{
// do stuff
}
it's very great because if you add more elements in the future it's gonna keep compiling even tho you are missing some
an alternative is to defined a global next to the const block but not everyone does this