英文:
What is the grpc proto for golang const?
问题
如果我有以下的golang代码:
type Animal string
const (
Cat Animal = "cat"
Dog Animal = "dog"
Mouse Animal = "mouse"
)
我应该如何编写proto文件?如果我使用枚举,那么我是否需要手动进行映射?
enum Animal {
CAT = 0;
DOG = 1;
MOUSE = 2;
}
英文:
If I have the golang
type Animal string
const (
Cat Animal = "cat"
Dog Animal = "dog"
Mouse Animal = "mouse"
)
How should I write the proto? If I use enums I will then need to manually map across myself?
enum Animal {
CAT = 0;
DOG = 1;
MOUSE = 2;
}
答案1
得分: 2
你可以动态访问,像这样:
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
英文:
You could access dynamically like:
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)
}
Will print
animal.Animal=CAT
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论