How do I use enums?

Viewed 841

I tried searching for a way to do this, but always found iota but I don't want the varible to have any value other than what I have specified.

Is there any other way to do it? Preferably one that acts like rust enums.

5 Answers

Is there any other way to do it?

You can use lookup table (key + value) in a database instead of using enums. In most cases I use an indexed integer for key and description in the value.

Which means that each key takes less space in the database. Plus the flexibility to add more "enums" if needed is easier. Besides this you can reuse the lookup tables in several tables.

This is not an alternative solution to your question, but effective go mentions:

In Go, enumerated constants are created using the iota enumerator.

https://go.dev/doc/effective_go#constants

Which would imply it is an acceptable approach. I'm not very familar with how rust does it, but maybe an example of the problem you have would help.