• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

enum constants in Go

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd appreciate any comments and feedback about what I did regarding enum constants in Go, as described below.

I tried implementing a Rock Paper Scissors Lizard Spock program in Go.  In my Java implementation, I defined an enum called Move to represent the possible moves one can make.

The constants are defined in that order to take advantage of the implicit ordinal() method in calculating the winner of a round.

In Go, I did this:

I decided to define LAST_Move because I couldn't find a way to limit the value of a Move to the range (0..4) since Move is basically an alias for the built-in type int. It seems if you define an "enum" in Go like this, you have to do your own range checking, which is why I added the NotLast() and InRange() methods for Move.  This is how I used NotLast():

And here's what MatchUp looks like:

And this is the output of SheldonExplains():

Scissors cuts Paper
Paper covers Rock
Rock crushes Lizard
Lizard poisons Spock
Spock smashes Scissors
Scissors decapitates Lizard
Lizard eats Paper
Paper disproves Spock
Spock vaporizes Rock
...and as it always has, Rock crushes Scissors
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing I don't like about the style of Go programs I have seen so far is what seems to be a preference for one-letter variables. I'm trying to follow that style but it's been against my better "clean code" judgement.

Can someone please comment on whether or not they follow the "terse but cryptic" style that seems to be favored in Go? Is this a de facto standard or are there folks out there who actually forego convention in favor of more expressive names in their Go programs?
 
permaculture is giving a gift to your future self. After reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic