-
-
Save var23rav/c71cccc649fc00c19fad08aaee52aad0 to your computer and use it in GitHub Desktop.
syntax = "proto3"; | |
package car; | |
import "common/vehicle_info.proto"; | |
message Car { | |
vehicle.Vehicle info = 1; | |
string carType = 2; | |
} | |
service Reader { | |
rpc GetPriceInfo(GetPriceInfoRequest) returns (GetPriceInfoResponse); | |
} | |
message GetPriceInfoRequest { | |
string manufacturer = 1; | |
string model = 2; | |
} | |
message GetPriceInfoResponse { | |
vehicle.PurchaseEntity info = 1; | |
} |
syntax = "proto3"; | |
package vehicle; | |
message PurchaseEntity { | |
string manufacturer = 1; | |
string model = 2; | |
int32 price = 3; | |
} | |
message Vehicle { | |
PurchaseEntity info = 1; | |
string engineType = 2; | |
int32 wheelCount = 3; | |
int32 registrationNumber = 4; | |
} |
Now to generate the go file of our targeted proto proto_folder\car\car_info.proto use the below command
protoc ^
--proto_path=./proto_folder ^
--go_out=./generated_codes ^
--go_opt=Mcommon/vehicle_info.proto=generated_code_with_go_opt/vehicle_pkg ^
--go_opt=Mcar/car_info.proto=generated_code_with_go_opt/car_pkg ^
car/car_info.proto
This will generate generated_codes\generated_code_with_go_opt\car_pkg\car_info.pb.go go file.
You could generate them together using this simple command but for that both proto should define the expected package path through go_package
option.
protoc ^
--proto_path=./proto_folder ^
--go_out=./generated_codes ^
car/car_info.proto ^
common/vehicle_info.proto
Otherwise as mentioned earlier include the go_opt option in protoc command.
protoc ^
--proto_path=./proto_folder ^
--go_out=./generated_codes ^
--go_opt=Mcommon/vehicle_info.proto=generated_code_with_go_opt/vehicle_pkg ^
common/vehicle_info.proto ^
--go_opt=Mcar/car_info.proto=generated_code_with_go_opt/car_pkg ^
car/car_info.proto
Step by step action can be found in the medium.
Note that i was using windows for my development thus the use of ^ for multi line command replace it with \ for linux based os.
The above method will only generate the message part of the proto.
If you want to generate you service code too, you have to do one more step.
protoc ^
--proto_path=./proto_folder ^
--go-grpc_out=./generated_codes ^
--go-grpc_opt=Mcar/car_info.proto=generated_code_with_go_opt/car_pkg ^
car/car_info.proto
They have separated these step into two to make the code/release more maintainable.
The difference,
generation of message usesgo_out
withgo_opt
.
but for service part we usego-grpc_out
withgo-grpc_opt
You can generate them together using..
protoc ^
--proto_path=./proto_folder ^
^
--go_out=./generated_codes ^
--go_opt=Mcommon/vehicle_info.proto=generated_code_with_go_opt/vehicle_pkg ^
--go_opt=Mcar/car_info.proto=generated_code_with_go_opt/car_pkg ^
^
--go-grpc_out=./generated_codes ^
--go-grpc_opt=Mcar/car_info.proto=generated_code_with_go_opt/car_pkg ^
^
common/vehicle_info.proto ^
car/car_info.proto
Here are some way to fix the error like 'protoc-gen-go: unable to determine Go import path for "common/vehicle_info.proto"' error while go file generation.
Generate with adding go_package in proto file itself
Update common/vehicle_info.proto file with
option go_package="generated_code_with_go_package/vehicle_pkg";
line.The earlier update allow us to generate the go file using the below command
This will generate go file in generated_codes\generated_code_with_go_package\vehicle_pkg
Alternatively you could mention the package path in go_opt option