如何在golang中按值获取枚举变量名?

如何在golang中按值获取枚举变量名?,go,Go,我使用enum来定义id和文本值之间的关系,但我使用id作为enum值,因为我不能使用id作为变量名 type Gender uint8 type MemberType uint8 const ( Male Gender = 2 Female Gender = 5 Standard MemberType = 2 VIP MemberType = 5 ) 现在我已经从性别表和成员类型表中选择了id 5,如何使用它来获取性别文本“女性”和成员类型文本“VIP”?

我使用enum来定义id和文本值之间的关系,但我使用id作为enum值,因为我不能使用id作为变量名

type Gender uint8
type MemberType uint8

const (
    Male Gender = 2
    Female Gender = 5

    Standard MemberType = 2
    VIP MemberType = 5
)

现在我已经从性别表和成员类型表中选择了id 5,如何使用它来获取性别文本“女性”和成员类型文本“VIP”?

将所选id转换为性别。例如:

selectedID:=5
selectedGender:=性别(selectedID)
fmt.Println(selectedGender==Female)//真
另一个SelectedId:=5
selectedMemberType:=成员类型(另一个SelectedId)
fmt.Println(selectedMemberType==VIP)//true

操场:

将所选id强制转换为
Gender
类型。例如:

selectedID:=5
selectedGender:=性别(selectedID)
fmt.Println(selectedGender==Female)//真
另一个SelectedId:=5
selectedMemberType:=成员类型(另一个SelectedId)
fmt.Println(selectedMemberType==VIP)//true

操场:

如果你想获得字符串“女性”和“VIP”

或:

然后你会有类似

 id := 5
 fmt.Println(genders[id]) // "Female"
 fmt.Println(memberTypes[id]) // "VIP"
 // or...
 fmt.Println(genders[Gender(id)]) // "Female"
 fmt.Println(memberTypes[MemberType(id)]) // "VIP"

如果你想得到字符串“女性”和“VIP”

或:

然后你会有类似

 id := 5
 fmt.Println(genders[id]) // "Female"
 fmt.Println(memberTypes[id]) // "VIP"
 // or...
 fmt.Println(genders[Gender(id)]) // "Female"
 fmt.Println(memberTypes[MemberType(id)]) // "VIP"
根据。 在
golang.org/x/tools/cmd/stringer

使用stringer,您可以这样做

/*enum.go*/
//go:生成梯边梁-类型=Pill
类型丸int
常数(
安慰剂药丸=iota
阿司匹林
布洛芬
醋氨酚
对乙酰氨基酚=对乙酰氨基酚
)
保存
enum.go
,然后运行
go generate
。斯特林格会为你做一切的

in the same directory will create the file pill_string.go, in package 
painkiller, containing a definition of

func (Pill) String() string

That method will translate the value of a Pill constant to the string
representation of the respective constant name, so that the call

fmt.Print(painkiller.Aspirin)

will print the string "Aspirin".
根据。 在
golang.org/x/tools/cmd/stringer

使用stringer,您可以这样做

/*enum.go*/
//go:生成梯边梁-类型=Pill
类型丸int
常数(
安慰剂药丸=iota
阿司匹林
布洛芬
醋氨酚
对乙酰氨基酚=对乙酰氨基酚
)
保存
enum.go
,然后运行
go generate
。斯特林格会为你做一切的

in the same directory will create the file pill_string.go, in package 
painkiller, containing a definition of

func (Pill) String() string

That method will translate the value of a Pill constant to the string
representation of the respective constant name, so that the call

fmt.Print(painkiller.Aspirin)

will print the string "Aspirin".
您可以为每个枚举类型使用单独的方法来获取字符串 价值观如下:-

游乐场网址:-

您可以为每个枚举类型使用单独的方法来获取字符串 价值观如下:-

游乐场网址:-


您正在尝试将包含
5
的变量转换为
性别
类型等于
女性
的变量,还是尝试将字符串
“女性”
放入变量中?我想将id转换为display@CLSo哪个是“身份证”?是“Male”还是“2”?2是id,Male是用于显示的文本go中没有枚举。您是否试图将包含
5
的变量转换为
Gender
类型等于
Female
的变量,还是试图将字符串
置于“Female”位置
在变量中?我想将id转换为文本display@CLSo哪个是“身份证”?是“男性”还是“2”?2是id,男性是用于显示的文本go中没有枚举我想将id转换为用于显示的文本我想将id转换为用于显示的文本我更喜欢
stringer
为我自动生成映射到字符串代码。我更喜欢
stringer
为我自动生成映射到字符串代码。
package main

import (
    "fmt"
)

type Gender uint8
type MemberType uint8

const (
    Male     Gender     = 2
    Female   Gender     = 5
    Standard MemberType = 2
    VIP      MemberType = 5
)

func (data Gender) String() string {
    switch data {
    case Male:
        return "Male"
    case Female:
        return "Female"
    default:
        return "unknown"
    }
}

func (data MemberType) String() string {
    switch data {
    case Standard:
        return "Standard"
    case VIP:
        return "VIP"
    default:
        return "unknown"
    }
}

func main() {
    fmt.Println(Male.String())
    fmt.Println(Female.String())
    fmt.Println(VIP.String())
    fmt.Println(Standard.String())
}