Hi, 

I've convert Egon's idea into a package, https://github.com/suntong/enum. 
It works fine for a single enum case, see the demo 
in https://github.com/suntong/enum. 

However, now I need to define different series of enums, and I'm wondering 
what the best way to do it. I tried to give the package different names, 
but that doesn't help (the new enum serie Weekday doesn't re-start from 
zero):


package main


import (
 "fmt"


 enum "github.com/suntong/enum"
 enum2 "github.com/suntong/enum"
)


var (
 Alpha = enum.Ciota("Alpha")
 Beta  = enum.Ciota("Beta")


 Sunday = enum2.Ciota("Sunday")
 Monday = enum2.Ciota("Monday")
)


type Example struct {
 enum.Enum
}


type Weekday struct {
 enum2.Enum
}


func main() {
 fmt.Printf("%+v\n", Example{Alpha})
 fmt.Printf("%+v\n", Example{Beta})
 fmt.Println("=======")
 fmt.Printf("%d\t%d\n", Alpha, Alpha+1)
 fmt.Printf("%+v\t%+v\n", Example{Beta - 1}, Example{Alpha + 1})
 fmt.Println("=======")
 if a, ok := enum.Get("Alpha"); ok {
 fmt.Printf("%d\n", a)
 }
 if b, ok := enum.Get("Beta"); ok {
 fmt.Printf("%d: %+v\n", b, Example{b})
 }
 fmt.Printf("%d:%+v\n", Sunday, Weekday{Sunday})
}





The output is, 

Alpha
Beta
=======
0       1
Alpha   Beta
=======
0
1: Beta
2:Sunday

thanks


On Thursday, November 28, 2013 at 5:29:04 AM UTC-5, Egon wrote:
>
> http://play.golang.org/p/O1ResE3yCx and 
> http://play.golang.org/p/f7HD7O0BNB
>
> + egon
>
> On Thursday, November 28, 2013 11:48:47 AM UTC+2, Péter Szilágyi wrote:
>>
>> Hi all,
>>
>>   This might be a bit unorthodox question, but I thought I'd ask away 
>> anyway :D Is there a straightforward possibility to assign string 
>> representations to an enumeration inline (i.e. at the point of declaring 
>> the enum), or possibly getting the original source code string back?
>>
>>   E.g. I have an enumeration like:
>>
>> const (
>>   Abc = iota
>>   Def
>>   Ghi
>>   Etc
>> )
>>
>>   And during debugging or just logging when I dump my structs containing 
>> these, I'd like to have a textual representation oppose to an int (mainly 
>> because I have many tens of entries, and manual lookup is bothersome).
>>
>>   Of course, I could map the ints to strings, but I'm wondering if there 
>> is something simpler, similar to tagging a struct field with a json name... 
>> i.e.
>>
>> type S struct{
>>   Address string `json:"address"`
>> }
>>
>>   If not, it's not really an issue, I'm just curious :)
>>
>> Thanks,
>>   Peter
>>
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to