Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

enum constants in Go

 
Sheriff
Posts: 14738
244
Android Mac IntelliJ IDE Eclipse IDE Spring Debian Ubuntu Java Linux
  • Mark post as helpful
  • send pies
  • 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: 14738
244
Android Mac IntelliJ IDE Eclipse IDE Spring Debian Ubuntu Java Linux
  • Mark post as helpful
  • send pies
  • 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?
 
Your mother was a hamster and your father was a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!