/* * Example of something like enumerated type in Go * @author gtowell * created: Aug 18, 2021 */ package main import "fmt" // Define a type, and limit it to integers type DogGroups int // Define the "members" of the type // This just makes a set of names but does not further bound the type const ( herding DogGroups = iota // a function that sets the value of the first item and then remaining items in the group hound nonsporting sporting terrier toy working ) // Start of a dog structure .. lots more needed type Dog struct { group DogGroups } // method on dog structure to return group name // Note that "toy" is missing but the compiler does not care. // Does go have fallthrough on switch -- not by default func (d Dog) GroupName() string { switch d.group { case herding: return "Herding" case hound: return "Hound" case nonsporting: return "ns" case sporting: return "sporting" case terrier: return "terrier" case working: return "working" case 42: return "perfect" default: return "unknown" } } func main() { fmt.Printf("Herding %v\n", herding) fmt.Printf("Hound %v\n", hound) dog1 := Dog{herding} dog42 := Dog{42} dog17 := Dog{group: 17} fmt.Printf("%s\n", dog1.GroupName()) fmt.Printf("%s\n", dog42.GroupName()) fmt.Printf("%s\n", dog17.GroupName()) }