Description
Proposal Details
stringer solves int->string case, making it easier to define enum-like values in Go.
It does not, however, generate a reversal index which is necessary for some (but not all) enums.
A string->int conversion method could be generated when a special flag -fromstring
is provided.
With FromString(s string) T
method, it would be possible to serialize enums to files and back without having to write that code yourself. The whole idea is to avoid the need to update two entities, int->string and string->int mappings. There are many ways to implement that mapping manually, but it also leads to a decision problem and inconsistency where a single large project may have different string->int conversion tactics (a map, a switch statement, a linear search along the constants, etc).
This would make my life easier, given that I already use stringer everywhere I can. I'm interested to know if anyone else is interested in this feature as well.
If this is not rejected right away, we can figure out the details. E.g. how FromString would behave with an invalid input (I would assume that it would not return an error, perhaps it would set the value to 0 as it's usually a convention for unknown/invalid/zero values in enum-like declarations)
Activity
gabyhelp commentedon Mar 19, 2025
Related Issues
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
seankhliao commentedon Mar 19, 2025
see all the previous issues linked above.