如果我有 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 關注
- 115 瀏覽
添加回答
舉報
0/150
提交
取消