How can I write an enum with "intellisence support"? - go

In go you can write an enum like this
type Direction int
const (
North Direction = iota
South
East
West
)
func main() {
// Declaring a variable myDirection with type Direction
var myDirection Direction
myDirection = West
if (myDirection == West) {
fmt.Println("myDirection is West:", myDirection)
}
}
Now image you write an enum which not only has 4 option, but instead 100. What I want is an enum that gives me "Inellisence support": If I type the enum, type a ., I want to know what options are there for the enum.
An example how this could look like is this. Is there a better way?
type direction struct{}
func (d *direction) north() string {
return "north"
}
func (d *direction) east() string {
return "east"
}
func (d *direction) south() string {
return "south"
}
func (d *direction) west() string {
return "west"
}
func main() {
var d direction
d.east()
...
}

I suggest to start the names of the enum values with a common prefix, e.g. Dir like this:
const (
DirNorth Direction = iota
DirSouth
DirEast
DirWest
)
Doing so, when you type packagename.Dir, you'll get a list of the possible values.
So beside applying a good naming strategy, you'll get improved auto-completion at the same time, and your source code becomes more readable (especially if there are a lot of enum values and you have more common words among them).
This is also used by the standard library, great examples are in the net/http package:
const (
MethodGet = "GET"
MethodHead = "HEAD"
MethodPost = "POST"
// ...
)
const (
StatusContinue = 100 // RFC 7231, 6.2.1
StatusSwitchingProtocols = 101 // RFC 7231, 6.2.2
StatusProcessing = 102 // RFC 2518, 10.1
StatusOK = 200 // RFC 7231, 6.3.1
StatusCreated = 201 // RFC 7231, 6.3.2
// ...
)
See related question: Glued acronyms and golang naming convention

Related

Working with bitmasks in Go (Go enumeration with iota)

Bit stuck.
I'm trying to work out how to get the bitmask values from a const with go enumeration. For example, if key is 5, which is 0101 bits, it would be dog and fish. What is the easiest way to get the bit values (1,2,4,8,16, 32, 64 etc) so I can select the string values and return the set of animals?
type Key int
const (
Dog Key = 1 << iota
Cat
Fish
Horse
Snake
Rabbit
Lion
Rino
Hedgehog
)
Been reading but I can't work this out.
Declare a slice of strings where the strings correspond to the constant names:
var animalNames = []string{
"Dog",
"Cat",
"Fish",
"Horse",
"Snake",
"Rabbit",
"Lion",
"Rino",
"Hedgehog",
}
To get the names for the bits, loop through the names. If the corresponding bit is set, add the name to the result:
func Names(k Key) []string {
var result []string
for i := 0; i < len(animalNames); i++ {
if k&(1<<uint(i)) != 0 {
result = append(result, animalNames[i])
}
}
return result
}
Run it on the playground.
If you change the the constants to be bit indexes instead of bit values, then you can use the stringer utility to create a func (k Key) String() string. Here's the code for this change:
type Key uint
const (
Dog Key = iota
Cat
Fish
Horse
Snake
Rabbit
Lion
Rino
Hedgehog
)
//go:generate stringer -type=Key
func Names(k Key) []string {
var result []string
for animal := Dog; animal <= Hedgehog; animal++ {
if k&(1<<animal) != 0 {
result = append(result, animal.String())
}
}
return result
}
Creating bitmask values with iota
Iota can be very useful when creating a bitmask. For instance, to represent the state of a network connection which may be secure, authenticated, and/or ready, we might create a bitmask like the following:
package main
import "fmt"
const (
Secure = 1 << iota // 0b001
Authn // 0b010
Ready // 0b100
)
// 0b011: Connection is secure and authenticated, but not yet Ready
func main() {
ConnState := Secure | Authn
fmt.Printf(` Secure: 0x%x (0b%03b)
Authn: 0x%x (0b%03b)
ConnState: 0x%x (0b%03b)
`, Secure, Secure, Authn, Authn, ConnState, ConnState)
}

Using bitsets in golang to represent capabilities

I am new to Golang and would like to model physical devices for measuring quantities such as light intensity, mass, electric current and so forth. So as a starting point I will define a device struct as follows:
const (
// Light can be measured in the form of luminosity
Light = 1<< iota
Mass
ElectricalCurrent
)
type PhysicalDevice struct{
Owner string
ID string
Description string
}
I am a confused now on how to express the device's capabilities (what it can measure) and the units of measurement. For example I would like to express that a physical device can measure electrical currents in amperes. However, I also want to express that a PhysicalDevice can measure more than one quantity. For example it could measure electrical current and temperature.
The PhysicalDevice's capabilities are not known in advance and can contain an arbitrary combination of capabilities.
I was thinking of using something equivalent to a C++ bitset for expressing the physical quantities a device can measure (would this be the right approach in the first place?).
I did not find the Go bitset type and not sure how to express that. I also need to map the measured physical quantity to a corresponding unit.
You should understand that trying to replicate another language's features in Go is generally regarded as a bad idea. There is a 'go' way of doing things.
You might want to consider iota and bitmask operations like this example on the Go playground. I have included the code here as well (in all its plagiarized glory):
package main
import "fmt"
func main() {
TestAddFlag()
TestHasFlag()
TestClearFlag()
TestToggleFlag()
fmt.Println("all tests passed")
}
type Bitmask uint32
func (f Bitmask) HasFlag(flag Bitmask) bool { return f&flag != 0 }
func (f *Bitmask) AddFlag(flag Bitmask) { *f |= flag }
func (f *Bitmask) ClearFlag(flag Bitmask) { *f &= ^flag }
func (f *Bitmask) ToggleFlag(flag Bitmask) { *f ^= flag }
const (
TESTFLAG_ONE Bitmask = 1 << iota
TESTFLAG_TWO
TESTFLAG_THREE
)
func TestAddFlag() {
var mainFlag Bitmask = TESTFLAG_TWO
mainFlag.AddFlag(TESTFLAG_THREE)
if mainFlag&(1<<TESTFLAG_THREE) != 0 {
panic("failed")
}
}
func TestClearFlag() {
var mainFlag Bitmask = TESTFLAG_ONE | TESTFLAG_THREE
mainFlag.ClearFlag(TESTFLAG_THREE)
if mainFlag&(1<<TESTFLAG_ONE) != 0 {
panic("failed")
}
}
func TestHasFlag() {
var mainFlag Bitmask = TESTFLAG_ONE | TESTFLAG_THREE
if !mainFlag.HasFlag(TESTFLAG_THREE) {
panic("failed")
}
}
func TestToggleFlag() {
flag := TESTFLAG_ONE | TESTFLAG_THREE
flag.ToggleFlag(TESTFLAG_ONE)
if flag.HasFlag(TESTFLAG_ONE) {
panic("failed")
}
flag.ToggleFlag(TESTFLAG_ONE)
if !flag.HasFlag(TESTFLAG_ONE) {
panic("failed")
}
}
This approach is commonly used in the standard library.
Define the capabilities using = 1 << iota:
const (
Light Capability = 1 << iota
Mass
ElectricalCurrent
Energy
)
Notice that the expression is only needed on the first constant. The same expression (but with an updated value of iota) will be used on the consecutive lines in the same group.
Here's a complete working example:
package main
import (
"fmt"
"strings"
)
type Capability int
const (
// Light can be measured in the form of luminosity
Light Capability = 1 << iota
Mass
ElectricalCurrent
Energy
)
func (c Capability) String() string {
var caps []string
if c&Light > 0 {
caps = append(caps, "Light")
}
if c&Mass > 0 {
caps = append(caps, "Mass")
}
if c&ElectricalCurrent > 0 {
caps = append(caps, "ElectricalCurrent")
}
if c&Energy > 0 {
caps = append(caps, "Energy")
}
return strings.Join(caps, "|")
}
type PhysicalDevice struct {
Owner string
ID string
Description string
Capability Capability
}
func (pd PhysicalDevice) String() string {
return "Owner: " + pd.Owner + "\n" +
"ID: " + pd.ID + "\n" +
"Description: " + pd.Description + "\n" +
"Capability: " + pd.Capability.String() + "\n"
}
func main() {
dev := PhysicalDevice{
Owner: "Albert Einstein",
ID: "E=mc^2",
Description: "My well-known formula as a device",
Capability: Energy | Mass | Light,
}
fmt.Println(dev)
}
The code can be found on The Go Playground.

Why can't I access this field in an interface?

I am trying to understand interfaces better and am not understanding why s has no field Width. My example is here:
package main
import "fmt"
type shapes interface {
setWidth(float64)
}
type rect struct {
Width float64
}
func (r *rect) setWidth(w float64) {
r.Width = w
}
var allShapes = map[string]shapes{
"rect": &rect{},
}
func main() {
r := &rect{}
r.setWidth(5)
fmt.Println(r.Width) // this works
for _, s := range allShapes {
s.setWidth(7)
fmt.Println(s.Width) // why not???
}
}
Why does r have Width but s doesn't? The exact error I get is:
s.Width undefined (type shapes has no field or method Width)
shapes interface is what *rect implements, but it is not a concrete type *rect. It is, like any interface, a set of methods allowing any type satisfying it to pass, like giving a temporary visitor sticker to it to go up the building.
For instance, if there is a monkey (or for what it's worth, a dolphin) who can act and do everything a human can, in Go's building, he can pass the guard and go up the elevator. However, that doesn't make him genetically human.
Go is statically-typed, meaning even two types with the same underlying type cannot be dynamically converted or coerced to one another without a type assertion or consciously converting the type.
var a int
type myInt int
var b myInt
a = 2
b = 3
b = a // Error! cannot use a (type int) as type myInt in assignment.
b = myInt(a) // This is ok.
Imagine with me for a second this situation:
type MyInt int
type YourInt int
type EveryInt interface {
addableByInt(a int) bool
}
func (i MyInt) addableByInt(a int) bool {
// whatever logic doesn't matter
return true
}
func (i YourInt) addableByInt(a int) bool {
// whatever logic doesn't matter
return true
}
func main() {
// Two guys want to pass as an int
b := MyInt(7)
c := YourInt(2)
// Do everything an `EveryInt` requires
// and disguise as one
bi := EveryInt(b)
ci := EveryInt(c)
// Hey, look we're the same! That's the closest
// we can get to being an int!
bi = ci // This is ok, we are EveryInt brotherhood
fmt.Println(bi) // bi is now 2
// Now a real int comes along saying
// "Hey, you two look like one of us!"
var i int
i = bi // Oops! bi has been made
// ci runs away at this point
}
Now back to your scenerio--imagine a *circle comes along implementing shapes:
type circle struct {
Radius float64
}
func (c *circle) setWidth(w float64) {
c.Radius = w
}
*circle is totally passable as shapes but it does not have Width property because it is not a *rect. An interface cannot access an underlying type's property directly, but can only do so through implemented method set. In order to access a property, a type assertion is need on the interface so that instance becomes a concrete type:
var r *rect
// Verify `s` is in fact a `*rect` under the hood
if r, ok := s.(*rect); ok {
fmt.Println(r.Width)
}
This is at the core of why a statically-typed language like Go is always faster than dynamically-typed counterparts, which will almost always use some kind of reflection to handle type coercion dynamically for you.
s is a an implementer of the shapes interface, but in the for loop not typed as a rect.
If you do a type assertion to force s be of the concrete type like so:
s.(*rect).Width
You will get what you want.
You need to be careful about mixing concrete types and interfaces like that though.

How to choose a random enumeration value

I am trying to randomly choose an enum value:
enum GeometryClassification {
case Circle
case Square
case Triangle
case GeometryClassificationMax
}
and the random selection:
let shapeGeometry = ( arc4random() % GeometryClassification.GeometryClassificationMax ) as GeometryClassification
but it fails.
I get errors like:
'GeometryClassification' is not convertible to 'UInt32'
How do I solve this?
In Swift there is actually a protocol for enums called CaseIterable that, if you add it to your enum, you can just reference all of the cases as a collection with .allCases as so:
enum GeometryClassification: CaseIterable {
case Circle
case Square
case Triangle
}
and then you can .allCases and then .randomElement() to get a random one
let randomGeometry = GeometryClassification.allCases.randomElement()!
The force unwrapping is required because there is a possibility of an enum having no cases and thus randomElement() would return nil.
Swift has gained new features since this answer was written that provide a much better solution — see "How to choose a random enumeration value" instead.
I'm not crazy about your last case there -- it seems like you're including .GeometryClassificationMax solely to enable random selection. You'll need to account for that extra case everywhere you use a switch statement, and it has no semantic value. Instead, a static method on the enum could determine the maximum value and return a random case, and would be much more understandable and maintainable.
enum GeometryClassification: UInt32 {
case Circle
case Square
case Triangle
private static let _count: GeometryClassification.RawValue = {
// find the maximum enum value
var maxValue: UInt32 = 0
while let _ = GeometryClassification(rawValue: maxValue) {
maxValue += 1
}
return maxValue
}()
static func randomGeometry() -> GeometryClassification {
// pick and return a new value
let rand = arc4random_uniform(_count)
return GeometryClassification(rawValue: rand)!
}
}
And you can now exhaust the enum in a switch statement:
switch GeometryClassification.randomGeometry() {
case .Circle:
println("Circle")
case .Square:
println("Square")
case .Triangle:
println("Triangle")
}
Since you're inside the enum class, having the random() method reference the highest value explicitly would eliminate having to count them every time:
enum GeometryClassification: UInt32 {
case Circle
case Square
case Triangle
static func random() -> GeometryClassification {
// Update as new enumerations are added
let maxValue = Triangle.rawValue
let rand = arc4random_uniform(maxValue+1)
return GeometryClassification(rawValue: rand)!
}
}
For Swift 5 there is "RandomNumberGenerator":
enum Weekday: CaseIterable {
case sunday, monday, tuesday, wednesday, thursday, friday, saturday
static func random<G: RandomNumberGenerator>(using generator: inout G) -> Weekday {
return Weekday.allCases.randomElement(using: &generator)!
}
static func random() -> Weekday {
var g = SystemRandomNumberGenerator()
return Weekday.random(using: &g)
}
}
You need to assign a raw type to your enum. If you use an integer type, then the enumeration case values will be auto-generated starting at 0:
enum GeometryClassification: UInt32 {
case Circle
case Square
case Triangle
case GeometryClassificationMax
}
Per Enumerations:
"Unlike C and Objective-C, Swift enumeration members are not assigned a default integer value when they are created."
Specifying the integer type lets it know to generate the values in the usual way.
Then you can generate the random value like this:
let randomEnum: GeometryClassification = GeometryClassification.fromRaw(arc4random_uniform(GeometryClassification.GeometryClassificationMax.toRaw()))!
This is a horribly ugly call, and all those fromRaw and toRaw calls are fairly inelegant, so I would really recommend generating a random UInt32 that is in the range you want first, then creating a GeometryClassification from that value:
GeometryClassification.fromRaw(someRandomUInt32)
You can put all the values into array and generate random,
extension GeometryClassification {
static func random() -> GeometryClassification {
let all: [GeometryClassification] = [.Circle,
.Square,
.Triangle,
.GeometryClassificationMax]
let randomIndex = Int(arc4random()) % all.count
return all[randomIndex]
}
}
The easiest thing to do is to create a global extension:
extension CaseIterable {
static func randomElement() -> AllCases.Element {
guard Self.allCases.count > 0 else {
fatalError("There must be at least one case in the enum")
}
return Self.allCases.randomElement()!
}
}
This way any enum which conforms to CaseIterable has the function automatically
Here's my Swift 1.2 take:
enum GeometryClassification : Int {
case Circle = 0
case Square = 1
case Triangle = 2
static func random() -> GeometryClassification {
let min = MutationType.Circle.rawValue
let max = MutationType.Triangle.rawValue
let rand = Int.random(min: min, max: max) // Uses ExSwift!
return self(rawValue: rand)!
}
}
I wrote a global extension using Andy's answer. Enjoy :)
extension CaseIterable {
static func random<G: RandomNumberGenerator>(using generator: inout G) -> Self.AllCases.Element {
return Self.allCases.randomElement(using: &generator)!
}
static func random() -> Self.AllCases.Element {
var g = SystemRandomNumberGenerator()
return Self.random(using: &g)
}
}
Just extend your enumeration to conform CaseIterable protocol and use like:
let state = YourEnum.random()

What is an idiomatic way of representing enums in Go?

I'm trying to represent a simplified chromosome, which consists of N bases, each of which can only be one of {A, C, T, G}.
I'd like to formalize the constraints with an enum, but I'm wondering what the most idiomatic way of emulating an enum is in Go.
Quoting from the language specs:Iota
Within a constant declaration, the predeclared identifier iota represents successive untyped integer constants. It is reset to 0 whenever the reserved word const appears in the source and increments after each ConstSpec. It can be used to construct a set of related constants:
const ( // iota is reset to 0
c0 = iota // c0 == 0
c1 = iota // c1 == 1
c2 = iota // c2 == 2
)
const (
a = 1 << iota // a == 1 (iota has been reset)
b = 1 << iota // b == 2
c = 1 << iota // c == 4
)
const (
u = iota * 42 // u == 0 (untyped integer constant)
v float64 = iota * 42 // v == 42.0 (float64 constant)
w = iota * 42 // w == 84 (untyped integer constant)
)
const x = iota // x == 0 (iota has been reset)
const y = iota // y == 0 (iota has been reset)
Within an ExpressionList, the value of each iota is the same because it is only incremented after each ConstSpec:
const (
bit0, mask0 = 1 << iota, 1<<iota - 1 // bit0 == 1, mask0 == 0
bit1, mask1 // bit1 == 2, mask1 == 1
_, _ // skips iota == 2
bit3, mask3 // bit3 == 8, mask3 == 7
)
This last example exploits the implicit repetition of the last non-empty expression list.
So your code might be like
const (
A = iota
C
T
G
)
or
type Base int
const (
A Base = iota
C
T
G
)
if you want bases to be a separate type from int.
Referring to the answer of jnml, you could prevent new instances of Base type by not exporting the Base type at all (i.e. write it lowercase). If needed, you may make an exportable interface that has a method that returns a base type. This interface could be used in functions from the outside that deal with Bases, i.e.
package a
type base int
const (
A base = iota
C
T
G
)
type Baser interface {
Base() base
}
// every base must fulfill the Baser interface
func(b base) Base() base {
return b
}
func(b base) OtherMethod() {
}
package main
import "a"
// func from the outside that handles a.base via a.Baser
// since a.base is not exported, only exported bases that are created within package a may be used, like a.A, a.C, a.T. and a.G
func HandleBasers(b a.Baser) {
base := b.Base()
base.OtherMethod()
}
// func from the outside that returns a.A or a.C, depending of condition
func AorC(condition bool) a.Baser {
if condition {
return a.A
}
return a.C
}
Inside the main package a.Baser is effectively like an enum now.
Only inside the a package you may define new instances.
You can make it so:
type MessageType int32
const (
TEXT MessageType = 0
BINARY MessageType = 1
)
With this code compiler should check type of enum
It's true that the above examples of using const and iota are the most idiomatic ways of representing primitive enums in Go. But what if you're looking for a way to create a more fully-featured enum similar to the type you'd see in another language like Java or Python?
A very simple way to create an object that starts to look and feel like a string enum in Python would be:
package main
import (
"fmt"
)
var Colors = newColorRegistry()
func newColorRegistry() *colorRegistry {
return &colorRegistry{
Red: "red",
Green: "green",
Blue: "blue",
}
}
type colorRegistry struct {
Red string
Green string
Blue string
}
func main() {
fmt.Println(Colors.Red)
}
Suppose you also wanted some utility methods, like Colors.List(), and Colors.Parse("red"). And your colors were more complex and needed to be a struct. Then you might do something a bit like this:
package main
import (
"errors"
"fmt"
)
var Colors = newColorRegistry()
type Color struct {
StringRepresentation string
Hex string
}
func (c *Color) String() string {
return c.StringRepresentation
}
func newColorRegistry() *colorRegistry {
red := &Color{"red", "F00"}
green := &Color{"green", "0F0"}
blue := &Color{"blue", "00F"}
return &colorRegistry{
Red: red,
Green: green,
Blue: blue,
colors: []*Color{red, green, blue},
}
}
type colorRegistry struct {
Red *Color
Green *Color
Blue *Color
colors []*Color
}
func (c *colorRegistry) List() []*Color {
return c.colors
}
func (c *colorRegistry) Parse(s string) (*Color, error) {
for _, color := range c.List() {
if color.String() == s {
return color, nil
}
}
return nil, errors.New("couldn't find it")
}
func main() {
fmt.Printf("%s\n", Colors.List())
}
At that point, sure it works, but you might not like how you have to repetitively define colors. If at this point you'd like to eliminate that, you could use tags on your struct and do some fancy reflecting to set it up, but hopefully this is enough to cover most people.
There is a way with struct namespace.
The benefit is all enum variables are under a specific namespace to avoid pollution.
The issue is that we could only use var not const
type OrderStatusType string
var OrderStatus = struct {
APPROVED OrderStatusType
APPROVAL_PENDING OrderStatusType
REJECTED OrderStatusType
REVISION_PENDING OrderStatusType
}{
APPROVED: "approved",
APPROVAL_PENDING: "approval pending",
REJECTED: "rejected",
REVISION_PENDING: "revision pending",
}
As of Go 1.4, the go generate tool has been introduced together with the stringer command that makes your enum easily debuggable and printable.
I am sure we have a lot of good answers here. But, I just thought of adding the way I have used enumerated types
package main
import "fmt"
type Enum interface {
name() string
ordinal() int
values() *[]string
}
type GenderType uint
const (
MALE = iota
FEMALE
)
var genderTypeStrings = []string{
"MALE",
"FEMALE",
}
func (gt GenderType) name() string {
return genderTypeStrings[gt]
}
func (gt GenderType) ordinal() int {
return int(gt)
}
func (gt GenderType) values() *[]string {
return &genderTypeStrings
}
func main() {
var ds GenderType = MALE
fmt.Printf("The Gender is %s\n", ds.name())
}
This is by far one of the idiomatic ways we could create Enumerated types and use in Go.
Edit:
Adding another way of using constants to enumerate
package main
import (
"fmt"
)
const (
// UNSPECIFIED logs nothing
UNSPECIFIED Level = iota // 0 :
// TRACE logs everything
TRACE // 1
// INFO logs Info, Warnings and Errors
INFO // 2
// WARNING logs Warning and Errors
WARNING // 3
// ERROR just logs Errors
ERROR // 4
)
// Level holds the log level.
type Level int
func SetLogLevel(level Level) {
switch level {
case TRACE:
fmt.Println("trace")
return
case INFO:
fmt.Println("info")
return
case WARNING:
fmt.Println("warning")
return
case ERROR:
fmt.Println("error")
return
default:
fmt.Println("default")
return
}
}
func main() {
SetLogLevel(INFO)
}
For a use case like this, it may be useful to use a string constant so it can be marshaled into a JSON string. In the following example, []Base{A,C,G,T} would get marshaled to ["adenine","cytosine","guanine","thymine"].
type Base string
const (
A Base = "adenine"
C = "cytosine"
G = "guanine"
T = "thymine"
)
When using iota, the values get marshaled into integers. In the following example, []Base{A,C,G,T} would get marshaled to [0,1,2,3].
type Base int
const (
A Base = iota
C
G
T
)
Here's an example comparing both approaches:
https://play.golang.org/p/VvkcWvv-Tvj
Here is an example that will prove useful when there are many enumerations. It uses structures in Golang, and draws upon Object Oriented Principles to tie them all together in a neat little bundle. None of the underlying code will change when a new enumeration is added or deleted. The process is:
Define an enumeration structure for enumeration items: EnumItem. It has an integer and string type.
Define the enumeration as a list of enumeration items: Enum
Build methods for the enumeration. A few have been included:
enum.Name(index int): returns the name for the given index.
enum.Index(name string): returns the name for the given index.
enum.Last(): returns the index and name of the last enumeration
Add your enumeration definitions.
Here is some code:
type EnumItem struct {
index int
name string
}
type Enum struct {
items []EnumItem
}
func (enum Enum) Name(findIndex int) string {
for _, item := range enum.items {
if item.index == findIndex {
return item.name
}
}
return "ID not found"
}
func (enum Enum) Index(findName string) int {
for idx, item := range enum.items {
if findName == item.name {
return idx
}
}
return -1
}
func (enum Enum) Last() (int, string) {
n := len(enum.items)
return n - 1, enum.items[n-1].name
}
var AgentTypes = Enum{[]EnumItem{{0, "StaffMember"}, {1, "Organization"}, {1, "Automated"}}}
var AccountTypes = Enum{[]EnumItem{{0, "Basic"}, {1, "Advanced"}}}
var FlagTypes = Enum{[]EnumItem{{0, "Custom"}, {1, "System"}}}
Refactored https://stackoverflow.com/a/17989915/863651 to make it a bit more readable:
package SampleEnum
type EFoo int
const (
A EFoo = iota
C
T
G
)
type IEFoo interface {
Get() EFoo
}
func(e EFoo) Get() EFoo { // every EFoo must fulfill the IEFoo interface
return e
}
func(e EFoo) otherMethod() { // "private"
//some logic
}
This is a safe way to implement enum in golang:
package main
import (
"fmt"
)
const (
MALE = _gender(1)
FEMALE = _gender(2)
RED = _color("RED")
GREEN = _color("GREEN")
BLUE = _color("BLUE")
)
type Gender interface {
_isGender()
Value() int
}
type _gender int
func (_gender) _isGender() {}
func (_g _gender) Value() int {
return int(_g)
}
type Color interface {
_isColor()
Value() string
}
type _color string
func (_color) _isColor() {}
func (_c _color) Value() string {
return string(_c)
}
func main() {
genders := []Gender{MALE, FEMALE}
colors := []Color{RED, GREEN, BLUE}
fmt.Println("Colors =", colors)
fmt.Println("Genders =", genders)
}
The output:
Colors = [RED GREEN BLUE]
Genders = [1 2]
Also, this is a pretty effective way to store different roles in one location in a byte, where the first value is set to 1, bit shifted by an iota.
package main
import "fmt"
const (
isCaptain = 1 << iota
isTrooper
isMedic
canFlyMars
canFlyJupiter
canFlyMoon
)
func main() {
var roles byte = isCaptain | isMedic | canFlyJupiter
//Prints a binary representation.
fmt.Printf("%b\n", roles)
fmt.Printf("%b\n", isCaptain)
fmt.Printf("%b\n", isTrooper)
fmt.Printf("%b\n", isMedic)
fmt.Printf("Is Captain? %v\n", isCaptain&roles == isCaptain)
fmt.Printf("Is Trooper? %v", isTrooper&roles == isTrooper)
}
I created the enum this way. Suppose we need an enum representing gender. Possible values are Male, Female, Others
package gender
import (
"fmt"
"strings"
)
type Gender struct {
g string
}
var (
Unknown = Gender{}
Male = Gender{g: "male"}
Female = Gender{g: "female"}
Other = Gender{g: "other"}
)
var genders = []Gender{
Unknown,
Male,
Female,
Other,
}
func Parse(code string) (parsed Gender, err error) {
for _, g := range genders {
if g.g == strings.ToLower(code) {
if g == Unknown {
err = fmt.Errorf("unknown gender")
}
parsed = g
return
}
}
parsed = Unknown
err = fmt.Errorf("unknown gender", code)
return
}
func (g Gender) Gender() string {
return g.g
}
A simpler way I have found to work.
const (
Stake TX = iota
Withdraw)
type TX int
func (t TX) String() string {
return [...]string{"STAKE", "WITHDRAW"}[t]}
log.Println(Stake.String()) --> STAKE

Resources