Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a "reverse" for the stringer tool?

Tags:

go

In go I can use stringer to convert const names to string:

//go:generate stringer -type=M
type M int

const (
    _ M = iota
    Foo // "Foo"
    Bar // "Bar"
)

Is there anything that would allow me to convert the "foo" string into a variable of type M except for a hand-written switch?

like image 779
andig Avatar asked Oct 25 '25 14:10

andig


1 Answers

I've recently discovered https://github.com/alvaroloes/enumer which adds a TypeString(string) Type method that will convert string value to type and can replace stringer.

like image 86
andig Avatar answered Oct 28 '25 04:10

andig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!