如果我有 golangtype Animal stringconst ( Cat Animal = "cat" Dog Animal = "dog" Mouse Animal = "mouse")我应该如何编写原型?如果我使用枚举,那么我需要手动映射自己吗?enum Animal { CAT = 0; DOG = 1; MOUSE = 2;}
1 回答
繁星coding
TA贡献1797条经验 获得超4个赞
您可以像这样动态访问:
import (
"fmt"
pb "animal"
)
func main() {
type Animal string
const (
Cat Animal = "CAT"
Dog Animal = "DOG"
Mouse Animal = "MOUSE"
)
myProtoAnimal := pb.Animal(pb.Animal_value[string(Cat)])
fmt.Printf("%T=%v", myProtoAnimal, myProtoAnimal)
}
将打印
animal.Animal=CAT
- 1 回答
- 0 关注
- 97 浏览
添加回答
举报
0/150
提交
取消