Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we assign custom names to enum values ? #25

Open
taherv opened this issue Nov 12, 2017 · 3 comments
Open

Can we assign custom names to enum values ? #25

taherv opened this issue Nov 12, 2017 · 3 comments

Comments

@taherv
Copy link

taherv commented Nov 12, 2017

type Fruit int
const (
     Apple Fruit = iota
     Banana
     Orange
)

I'd like the strings to be "apple", "banana", "orange". How can I assign custom strings ?

Something like this would work ?

type Fruit int
const (
     Apple Fruit = iota  `"apple"`
     Banana                  `"banana"`
     Orange                  `"orange"`
)
@taherv
Copy link
Author

taherv commented Nov 12, 2017

Also, for other reasons, I also need to implement stringer for my type, should jsonenums also generate the String() function ?

@dmitshur
Copy link
Contributor

I also need to implement stringer for my type

There exists stringer for that task. (But I don’t think it’s output is customizable.)

@taherv
Copy link
Author

taherv commented Nov 12, 2017

I found this project https://github.com/alvaroloes/enumer
It generates yaml/json marshalers and also stringer implementations all using the same strings for enums.
It has basic string customization support (they call it "transform") which does what I want (small case strings) but it is not generally customizable as I would have expected ..... it does the job for me in the time being

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants