Examples

Examples

Explore different usage patterns and features with practical examples:

This example demonstrates an extended enum type with custom fields:

package solarsystem

type planet int // Gravity float64,RadiusKm float64,MassKg float64,OrbitKm float64

//go:generate goenums planets.go
const (
    unknown planet = iota // invalid
    mercury               // Mercury 0.378,2439.7,3.3e23,57910000
    venus                 // Venus 0.907,6051.8,4.87e24,108200000
    earth                 // Earth 1,6378.1,5.97e24,149600000
    mars                  // Mars 0.377,3389.5,6.42e23,227900000
    jupiter               // Jupiter 2.36,69911,1.90e27,778600000
    saturn                // Saturn 0.916,58232,5.68e26,1433500000
    uranus                // Uranus 0.889,25362,8.68e25,2872500000
    neptune               // Neptune 1.12,24622,1.02e26,4495100000
)

After generation, we can use the extended enum type:

type planetHandler struct{}

func (planetHandler) Mercury() {}
func (planetHandler) Venus()   {}
func (planetHandler) Earth()   {}
func (planetHandler) Mars()    {}
func (planetHandler) Jupiter() {}
func (planetHandler) Saturn()  {}
func (planetHandler) Uranus()  {}
func (planetHandler) Neptune() {}

var _ solarsystem.PlanetMatcher = planetHandler{}

earthWeight := 100.0
fmt.Printf("Weight on %s: %.2f kg\n", 
    solarsystem.Planets.MARS, 
    earthWeight * solarsystem.Planets.MARS.Gravity)

// Iteration
for p := range solarsystem.Planets.All() {
    fmt.Printf("Planet: %s\n", p)
}

// Process every valid enum value
solarsystem.ExhaustivePlanets(func(p solarsystem.Planet) {
    fmt.Printf("Planet: %s\n", p)
})

// Compile-time exhaustive matching with runtime dispatch
if err := solarsystem.MatchPlanet(solarsystem.Planets.NEPTUNE, planetHandler{}); err != nil {
    // matcher was nil or the enum value was not handled
}

For more examples, see the testdata directory.