goでprotobufを生成する【go】

初めに

goでgRPCを試す際に protoファイルからgoファイルを生成するときにはまったので、メモしておきます

環境

  • go version go1.17.13 darwin/arm64

問題

以下のコマンドでprotoファイルから生成しようとしたが、エラーが出て生成されない

生成コマンド

protoc --go_out=. --go_opt=paths=source_relative \
    --go-grpc_out=. --go-grpc_opt=paths=source_relative \
    user.proto

エラー文

rotoc-gen-go: unable to determine Go import path for "user.proto"

Please specify either:
    • a "go_package" option in the .proto source file, ora "M" argument on the command line.

See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.

--go_out: protoc-gen-go: Plugin failed with status code 1.

解決方法

protoファイル側に以下の一文を追加する

option go_package = "github.com/ayutaz/protoc-go-experiments/helloworld";

参考サイト

zenn.dev