premise
All components are installed, including:
protoc
protoc-gen-go
protoc-gen-grpc-gateway
protoc-gen-swagger
How to install another article
It is divided into three parts:
- Compilation
- Compilation
- Compilation
Prepare the file first
syntax = "proto3"; package hello; import "google/api/"; service HelloWorld { rpc SayHelloWorld(HelloWorldRequest) returns (HelloWorldResponse) { option () = { post: "/hello_world" body: "*" }; } } message HelloWorldRequest { string referer = 1; } message HelloWorldResponse { string message = 1;
1. Compile the file
Need to useprotoc-gen-go
Component, command:
protoc --go_out=plugins=grpc:. ${}
Be careful to use-I
Parameter introduction related dependencies, such as heregoogle/api/
:
protoc -I ./ -I $GOPATH/src -I $GOPATH/src/google/api --go_out=plugins=grpc:.
The first-I
Introduce the current directory (because you need to use it);
The second one-I
Introduce relevant dependencies of go;
Third-I
IntroducedThe prerequisite for using this file is
$GOPATN/src
The relevant packages have been prepared;
Complete command:
protoc -I ./ -I $GOPATH/src -I $GOPATH/src/google/api --go_out=plugins=grpc:. ./
Output:
$ protoc -I ./ -I $GOPATH/src -I $GOPATH/src/google/api --go_out=plugins=grpc:. ./
protoc-gen-go: unable to determine Go import path for ""
Please specify either:
• a "go_package" option in the .proto source file, or
• a "M" argument on the command line.
See /protocol-buffers/docs/reference/go-generated#package for more information.
--go_out: protoc-gen-go: Plugin failed with status code 1.
It needs to be inSpecified in the file
go_package
Parameters, set to the current directory here, updatecontent:
syntax = "proto3"; package hello; option go_package="./"; import "google/api/"; service HelloWorld { rpc SayHelloWorld(HelloWorldRequest) returns (HelloWorldResponse) { option () = { post: "/hello_world" body: "*" }; } } message HelloWorldRequest { string referer = 1; } message HelloWorldResponse { string message = 1; }
Compile again and successfully generateddocument.
2. Compile the file
Need to useprotoc-gen-grpc-gateway
Component, command:
protoc -I ./ -I $GOPATH/src -I $GOPATH/src/google/api --grpc-gateway_out=logtostderr=true:.
run:
protoc -I ./ -I $GOPATH/src -I $GOPATH/src/google/api --grpc-gateway_out=logtostderr=true:. ./
At this time, the command line will output a large string of things, the file generation fails, and it is marked at the end:
...
--grpc-gateway_out: 11:1: expected 'IDENT', found 'import'
I tried many methods and finally found thatgo_package
Parameters cannot be written./
, add the suffix package name, for example, change it to./hello
, I don't know if the real reason is this, but after this modification, the compile and pass.
At this time, the completeas follows:
syntax = "proto3"; package hello; option go_package="./hello"; import "google/api/"; service HelloWorld { rpc SayHelloWorld(HelloWorldRequest) returns (HelloWorldResponse) { option () = { post: "/hello_world" body: "*" }; } } message HelloWorldRequest { string referer = 1; } message HelloWorldResponse { string message = 1; }
Rerun the above command and successfully generated
The file, the directory is as follows:
.
├── hello
│ └──
├──
└──
3. Compile the file
Need to useprotoc-gen-swagger
Component, command:
protoc -I ./ -I $GOPATH/src -I $GOPATH/src/google/api --swagger_out=logtostderr=true:.
run:
protoc -I ./ -I $GOPATH/src -I $GOPATH/src/google/api --swagger_out=logtostderr=true:. ./
Become successful, generatedocument.
Summarize
At this point, all three files have been successfully generated. So, organize the directory files as follows:
.
├──
├──
├──
└──
Several pitfalls were taken:
- How to solve Google/api/
- How to deal with the introduction of external proto files by proto
- Gateway generation failed to report an error
Although there are still some areas that have not been figured out, at least they have been solved and recorded for learning.
The above is the detailed content of how to use the proto component to compile files. For more information about go proto component compilation, please follow my other related articles!