时间久了不用就会忘记指令,这里做个笔记
创建一个文件
// +build tools
package tools
import (
_ "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway"
_ "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2"
_ "google.golang.org/grpc/cmd/protoc-gen-go-grpc"
_ "google.golang.org/protobuf/cmd/protoc-gen-go"
)
然后执行go mod tidy,相当于执行了下面的命令
go install \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 \
google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc
查看go env指令下gobin的目录,是否安装成功这几个文件
-rwxr-xr-x 1 root root 9222148 Jul 18 2023 protoc-gen-go
-rwxr-xr-x 1 root root 8803881 Jul 18 2023 protoc-gen-go-grpc
-rwxr-xr-x 1 root root 11999231 Feb 6 09:26 protoc-gen-grpc-gateway
创建测试的proto
syntax = "proto3";
package pb;
option go_package = "hook/pb";
import "google/api/annotations.proto";
message StringMessage {
string value = 1;
}
service YourService {
rpc Echo(StringMessage) returns (StringMessage) {
option (google.api.http) = {
post: "/v1/example/echo"
body: "*"
};
}
}
引入annotations.proto文件,因为添加的注解依赖该文件。
该文件需要手动从 https://github.com/googleapis/googleapis 仓库下载到自己的项目中。
该文件需要手动从 https://github.com/googleapis/googleapis 仓库下载到自己的项目中。
该文件需要手动从 https://github.com/googleapis/googleapis 仓库下载到自己的项目中.目录在https://github.com/googleapis/googleapis/tree/master/google/api
或者去自己的gopath目录下的pkg/mod下去找 ~/go/pkg/mod/github.com/grpc-ecosystem/grpc-gateway/v2@v2.7.0/protoc-gen-openapiv2/options,然后创建目录并复制过来
annotations.pb.go annotations.proto annotations.swagger.json BUILD.bazel openapiv2.pb.go openapiv2.proto openapiv2.swagger.json
目录结构
proto
├── google
│ └── api
│ ├── annotations.proto
│ └── http.proto
└── helloworld
└── my.proto
执行指令,因为我在同级目录下,所以我用的路径都是"."
protoc --proto_path=. --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative --grpc-gateway_out=. --grpc-gateway_opt=paths=source_relative my.proto
标签:protoc,proto,Grpc,gen,Golang,grpc,gateway,go,Gateway
From: https://www.cnblogs.com/qcy-blog/p/18009202