enums for golang with constants and iota

1,731 views
Skip to first unread message

Tong Sun

unread,
Apr 23, 2014, 11:55:20 AM4/23/14
to golan...@googlegroups.com
Hi, 

I know that golang doesn't have enums but use the constants and iota to simulate it. 

What I feel missing and still haven't figured out is that, in C, enums and variables are associated. I.e., when associate a variable with enum values, we know for sure that such variable will take no values other than the ones from defined enums. What's the best way to establish such association in go? 

For example, can I define a groups of constants like the following, and express the idea that my variable myWeekDay takes nothing but the such constants? I know it might be hard to enforce it, so I think establishing such logical association is more important.

Thanks
const (
	Sunday = iota
	Monday
	Tuesday
	Wednesday
	Thursday
	Friday
	Saturday
	numberOfDays  // this constant is not exported
)




Caleb Spare

unread,
Apr 23, 2014, 12:03:44 PM4/23/14
to Tong Sun, golang-nuts
You can make your own type instead of using untyped integer constants.

http://play.golang.org/p/wHQ-tHpHVA

You could still make a bad constant -- weekday(1000) -- but it's a lot
harder to do by accident. Also if the type is not exported than
package users cannot make bad constants.

-Caleb
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

minux

unread,
Apr 23, 2014, 12:06:45 PM4/23/14
to Tong Sun, golang-nuts
On Wed, Apr 23, 2014 at 11:55 AM, Tong Sun <sunto...@gmail.com> wrote:
I know that golang doesn't have enums but use the constants and iota to simulate it. 

What I feel missing and still haven't figured out is that, in C, enums and variables are associated. I.e., when associate a variable with enum values, we know for sure that such variable will take no values other than the ones from defined enums. What's the best way to establish such association in go? 
Actually in C you can assign an enum variable with any integer representable by the
integer type that hosts the enum.

$ gcc -c -o /dev/null -Wall -Wextra -x c - <<EOF
> enum { A, B, C } x = 10;
> EOF
$ echo $?
0

For example, can I define a groups of constants like the following, and express the idea that my variable myWeekDay takes nothing but the such constants? I know it might be hard to enforce it, so I think establishing such logical association is more important.
there really isn't a way to do this perfectly in Go, as you can always use ideal consts.

Caleb Spare

unread,
Apr 23, 2014, 12:08:24 PM4/23/14
to minux, Tong Sun, golang-nuts
Oh yeah, this is a good point.

minux

unread,
Apr 23, 2014, 12:08:56 PM4/23/14
to Caleb Spare, Tong Sun, golang-nuts
On Wed, Apr 23, 2014 at 12:03 PM, Caleb Spare <ces...@gmail.com> wrote:
You can make your own type instead of using untyped integer constants.

http://play.golang.org/p/wHQ-tHpHVA
it seems you forgot the type in const declarations: http://play.golang.org/p/6VD1RfPcQ_ 

You could still make a bad constant -- weekday(1000) -- but it's a lot
harder to do by accident. Also if the type is not exported than
package users cannot make bad constants.
they still can make arbitrary constants typed weekday.
for example, pkg.Monday * 1000.
and with some work, you can even get arbitrary variable number typed weekday.

Tong Sun

unread,
Apr 23, 2014, 12:27:41 PM4/23/14
to golan...@googlegroups.com, Caleb Spare, Tong Sun


On Wednesday, April 23, 2014 12:08:56 PM UTC-4, minux wrote:
Actually in C you can assign an enum variable with any integer representable by the
integer type that hosts the enum.

 I know it is hard to enforce it, and I don't care about the enforcement. Establishing a logical association is more important to me. 
 
On Wed, Apr 23, 2014 at 12:03 PM, Caleb Spare <ces...@gmail.com> wrote:
You can make your own type instead of using untyped integer constants.

http://play.golang.org/p/wHQ-tHpHVA
it seems you forgot the type in const declarations: http://play.golang.org/p/6VD1RfPcQ_ 

 Yep, that exactly expresses it. 

Thanks

Tong Sun

unread,
Apr 23, 2014, 12:34:49 PM4/23/14
to golan...@googlegroups.com, Caleb Spare, Tong Sun


On Wednesday, April 23, 2014 12:27:41 PM UTC-4, Tong Sun wrote:

 Yep, that exactly expresses it. 
 
Hmm, a bit more help please -- please correct this: http://play.golang.org/p/jJpPLouAqj


Martin Drlík

unread,
Apr 23, 2014, 12:46:44 PM4/23/14
to golan...@googlegroups.com, Caleb Spare, Tong Sun

dja...@gmail.com

unread,
Apr 23, 2014, 12:49:32 PM4/23/14
to golan...@googlegroups.com, Caleb Spare, Tong Sun

Tong Sun

unread,
Apr 23, 2014, 1:48:24 PM4/23/14
to golan...@googlegroups.com, Caleb Spare, Tong Sun, dja...@gmail.com


On Wednesday, April 23, 2014 12:49:32 PM UTC-4, dja...@gmail.com wrote:

 
Reply all
Reply to author
Forward
0 new messages