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]
Similar Posts:
- [Solved] Grpc Error: unable to determine Go import path for
- Error message added by protobuf: type already exists
- [How to Solve] protoc: Command not found
- Go Language compilation and installation protobuf: protoc-gen-go: program not found or is not executable
- [Solved] Protobuf & cmake Low Version Error: Could not find a package configuration file provided by “Protobuf” with any of the following names: ProtobufConfig.cmake
- Error: install_ FAILED_ UPDATE_ INCOMPATIBLE
- Android: installation error: install_ FAILED_ UPDATE_ Incompatible solution
- C# proto Import Files Error: File not found. Import “common.proto” was not found or had errors.
- [Solved] JAVA Run Error: Error: Could not find or load main class
- How to Solve Python TypeError: ‘module’ object is not callable