--
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...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Hi,Thanks for the variations. Appreciate it :). Alas, the first one was specifically what I wanted to avoid (it would also be simpler with a map[enum]string).
The second version however is close to what I intended, but it does not scale to multiple enums (other than having multiple iota functions or adding a second parameter). So although workable, it adds a bit too much complexity in my opinion.
--
Actually, I'm wondering if we have discussed tags for constants, which could solve the problem nicely.for example:const (Abc = iota `Abc`Def `Def`Ehg `Ehg`)
I should have set a type for the constants in my example. with this proposal, types with tagged constants won'tbe compile time concept anymore. We can use reflect on them.
type T int
const (
X T = 0 `X`
Y T = iota `Y`
Z `Z`
)
func (t T) String() string { /* reflective implementation here */ }
func main() { fmt.Println(Y) }
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?
It doesn't really seem worth it. Having written some simple enum -> string (big switch) functions, it's pretty easy and quick to do by hand for reasonably sized enums. The downsides are I guess that it's easy to get out of sync (adding new enum values and forgetting to update the enum -> string function) and doing it for very large enums is tedious. Of course, it would be pretty trivial to write a small go tool to generate these functions.
On Mon, Dec 2, 2013 at 3:20 AM, Kevin Gillette <extempor...@gmail.com> wrote:
What would happen with the following?type T int
const (
X T = 0 `X`
Y T = iota `Y`
Z `Z`
)
func (t T) String() string { /* reflective implementation here */ }
func main() { fmt.Println(Y) }The T is a stringer, so the usual thing will happen, the String() method will be invokedand its result printed.
package main
import ( "fmt" "reflect")
type CountryType struct { Asia int32 America int32 Europe int32}
func (c *CountryType) GetName(v int) string { return CountryReflectType.Field(v).Name}
var CountryEnum = CountryType{0,1,2}var CountryReflectType = reflect.TypeOf(CountryEnum)
func main() { fmt.Println(CountryEnum.GetName(1)); fmt.Println(CountryEnum.Europe);}
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})
}
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
)
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))
}
--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
package main | |
import ( | |
"fmt" | |
"github.com/mkideal/cli" | |
"github.com/suntong/enum" | |
) | |
var ( | |
gender enum.Enum | |
male = gender.Iota("male") | |
female = gender.Iota("female") | |
theGender int = -1 | |
) | |
type argT struct { | |
cli.Helper | |
Age int `cli:"age" usage:"your age"` | |
Gender string `cli:"g,gender" usage:"your gender (male/female)" dft:"male"` | |
} | |
// Validate implements cli.Validator interface | |
func (argv *argT) Validate(ctx *cli.Context) error { | |
if argv.Age < 0 || argv.Age > 300 { | |
return fmt.Errorf("age %d out of range", argv.Age) | |
} | |
ok := false | |
if theGender, ok = gender.Get(argv.Gender); !ok { | |
return fmt.Errorf("invalid gender %s", ctx.Color().Yellow(argv.Gender)) | |
} | |
return nil | |
} | |
func main() { | |
cli.Run(new(argT), func(ctx *cli.Context) error { | |
ctx.JSONln(ctx.Argv()) | |
fmt.Printf("%d:%s\n", theGender, gender.String(theGender)) | |
return nil | |
}) | |
} |
$ go run 010-validatorEnum.go
{"Help":false,"Age":0,"Gender":"male"}
0:male
$ go run 010-validatorEnum.go -g male
{"Help":false,"Age":0,"Gender":"male"}
0:male
$ go run 010-validatorEnum.go -g female
{"Help":false,"Age":0,"Gender":"female"}
1:female
$ go run 010-validatorEnum.go -g unknown
ERR! invalid gender unknown