person. Proto file
//Specify the version
//Note that proto3 is written a bit differently than proto2
syntax = "proto3";
//package name, when go file is generated by protoc
option go_package="/address2";
//phone type
//the first field of the enum type must be 0
enum PhoneType {
HOME = 0;
WORK = 1;
}
//Phone
message Phone {
PhoneType type = 1;
string number = 2;
}
//Person
message Person {
//The number after it indicates the identification number
int32 id = 1;
string name = 2;
//repeated means repeatable
//can have more than one phone, list type
repeated Phone phones = 3;
repeated phones = 3; }
//contact book
message ContactBook {
repeated Person persons = 1;
}
Explain in detail
rotoc -I=. /proto --go_out=. . /proto/*
protoc -I=$SRC_DIR --go_out=$DST_DIR $SRC_DIR/addressbook.proto
Result.
The file helloworld.pb.go is generated in the /proto directory
Here option go_package defines the path /proto to be imported, and -go_out also defines the path, so the final -go_out=.
Parameters
-I: directory of the source file (can be omitted)
--go_out: set the output directory of the generated Go code
The last argument indicates the source file
Grpc caused an error
If option go is not added to the proto file_package = “/proto”; This industry will report the following mistakes.
protoc-gen-go: unable to determine Go import path for "proto/helloworld.proto"
Please specify either:
• a "go_package" option in the .proto source file, or
• a "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.
The reason is the compatibility of different versions of protocol Gen go.
Solution:
First, add option go to the proto file_package = “/proto”;
Second, the old version of proto Gen go is adopted, and the command is used to switch to V1 Version 3.2 go get – u GitHub com/golang/protobuf/protoc-gen- [email protected]