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 + Egon On Tuesday, 25 April 2017 22:24:47 UTC+3, Tong Sun wrote: > > Hi, > > I've convert Egon's idea into a package, https://github.com/suntong/enum > <https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fsuntong%2Fenum&sa=D&sntz=1&usg=AFQjCNFrFM2tIWem8xGA3ZHXXy2NsIwYUw>. > > 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.