Here is an example:
package main
type State int
const (
Created State = iota
Modified
Deleted
)
func main() {
// Some code here where I need the list
// of all available constants of this type.
}
The use case for this is to create a Finite State Machine (FSM). Being able to get all constants will help me in writing a test case that will ensure that every new value has a corresponding entry in the FSM map.
If your constants are all in an order, you can use this:
You can also cache the output in e.g.
init(). This will only work when all constants are initialised withiotain order. If you need something that works for all cases, use an explicit slice.