Home > OS >  What is the grpc proto for golang const?
What is the grpc proto for golang const?

Time:01-07

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;
}

CodePudding user response:

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

  •  Tags:  
  • Related