Help us understand the problem. What is going on with this article?

protobufのenumをgolangの型(int32)に変換する

protobuf で定義した enum を golang handler 内で int32型 に変換したかった時
「protobuf enum int 変換」みたいなワードでササっとググって出てこなかったのでメモとして残しておきます。

結論としては、単純に変換したい型で囲ってあげればよい。
type(enum type)

[protobuf]

service User {
  rpc PostUser(PostUserRequest) returns (User) {}
}

enum SexType {
  OTHER  = 0;
  MALE   = 1;
  FEMALE = 2;
}

message User {
  int64 id = 1;
  string name = 2;
  SexType sex = 3;
}

message PostUserRequest{
  string name = 1;
  SexType sex = 2;
}
[golang handler]
func GetUser(ctx context.Context, req *user.PostUserRequest) (*user.User, error) {
    var sex int32
    sex = int32(req.SexType)
    ... something ...
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした