Stringer has knowledge of the set of values in a Go enumeration, with which the String() function is generated.
A more general use-case is to see String() as one of several enumeration support functions. This could add really useful enumerations to Go, based on the pattern of declaring a type based on int and a corresponding set of constants of the type.
We would need:
- func (s Xxx) String() string - as exists
- func AllXxxs() [Xxx] - returns a slice containing all const values
- func XxxOf(string) (Xxx, err) - returns the value matched from a string, if possible
The two new functions are natural extensions of stringer because they're based on having parsed the source code. However, they broaden its purpose considerably.
Stringer has knowledge of the set of values in a Go enumeration, with which the String() function is generated.
A more general use-case is to see String() as one of several enumeration support functions. This could add really useful enumerations to Go, based on the pattern of declaring a type based on
intand a corresponding set of constants of the type.We would need:
The two new functions are natural extensions of
stringerbecause they're based on having parsed the source code. However, they broaden its purpose considerably.