On Tuesday, April 25, 2017 at 4:25:14 PM UTC-4, Tong Sun wrote:
>
>
> On Tue, Apr 25, 2017 at 3:42 PM, Egon wrote:
>
> I think the extra "enum" package would reduce readability. The code you 
>> are putting into package is ~10 lines of code... so the extra package 
>> doesn't reduce much typing, but it also loses enum typing... Depending on 
>> the enum, you may want to have different properties as well...
>> I chose the name "ciota" because of "custom-iota", in practice you would 
>> want something more descriptive such as "NewWeekday". 
>
>
> So the solution is easy, don't create a general purpose package for it :D
>>
>
> OK. 
>
> If going that route, and I have several enum series to define, I need to 
> define a series of almost identical functions, right? Hmm..., that'd be 
> really messy. 
>
> Ok, I'll think of a better way on my own, which I doubt I could, :-)
>

 
Thought I need to use reflection, but after several round of wrestling, I 
managed to do without reflection at all:


package main


import (
 "fmt"


 enum "github.com/suntong/enum"
)


var (
 example enum.Enum
 Alpha   = example.Iota("Alpha")
 Beta    = example.Iota("Beta")


 weekday enum.Enum
 Sunday  = weekday.Iota("Sunday")
 Monday  = weekday.Iota("Monday")
)


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


 fmt.Printf("%d:%s\n", Sunday, weekday.String(Sunday))
 fmt.Printf("%d:%s\n", Monday, weekday.String(Monday))
}


The output:

Alpha
Beta
=======
0       1
Alpha   Beta
=======
0: Alpha
1: Beta
0:Sunday
1:Monday

Ref: 

https://github.com/suntong/lang/blob/master/lang/Go/src/ds/EnumsStr1p.go

and

https://github.com/suntong/enum


-- 
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