protoc --go_out=. *.proto
只会生成
protoc --go_out=. --go_opt=paths=source_relative \ --go-grpc_out=. --go-grpc_opt=paths=source_relative \ helloworld/helloworld.proto
下面是gRPC官方文档翻译 地址https://grpc.io/docs/languages/go/quickstart/
去,Go的三个最新主要 发行版中的任何一个。
有关安装说明,请参阅Go入门。 指导。
协议缓冲区编译器,protoc
,第3版。
有关安装说明,请参阅“协议缓冲区编译器安装”。
用于协议编译器的Go插件:
使用以下命令为Go安装协议编译器插件:
$ export GO111MODULE=on # Enable module mode
$ go get google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc
更新您的,PATH
以便protoc
编译器可以找到插件:
$ export PATH="$PATH:$(go env GOPATH)/bin"
示例代码是grpc-go的一部分 回购。
将回购文件下载为zip文件 并解压缩,或克隆存储库:
$ git clone -b v1.35.0 https://github.com/grpc/grpc-go
转到快速入门示例目录:
$ cd grpc-go/examples/helloworld
从examples/helloworld
目录:
编译并执行服务器代码:
$ go run greeter_server/main.go
在另一个终端上,编译并执行客户端代码以查看客户端输出:
$ go run greeter_client/main.go
Greeting: Hello world
恭喜你!您刚刚使用gRPC运行了客户端服务器应用程序。
在本节中,您将使用其他服务器方法来更新应用程序。gRPC服务是使用协议缓冲区定义的。要了解有关如何在.proto
文件中定义服务的更多信息,请参见基础教程。现在,您只需要知道服务器和客户端存根都有一个SayHello()
RPC方法,该方法HelloRequest
从客户端获取 参数并HelloReply
从服务器返回a ,并且该方法的定义如下:
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
打开helloworld/helloworld.proto
并添加SayHelloAgain()
具有相同请求和响应类型的新方法:
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
// Sends another greeting
rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
记住要保存文件!
在使用新的服务方法之前,您需要重新编译更新的 .proto
文件。
仍在examples/helloworld
目录中时,运行以下命令:
$ protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
helloworld/helloworld.proto
这将重新生成helloworld/helloworld.pb.go
和helloworld/helloworld_grpc.pb.go
文件,其中包含:
示例代码是grpc-go的一部分 回购。
从examples/helloworld
目录:
恭喜你!您刚刚使用gRPC运行了客户端服务器应用程序。
在本节中,您将使用其他服务器方法来更新应用程序。gRPC服务是使用协议缓冲区定义的。要了解有关如何在.proto
文件中定义服务的更多信息,请参见基础教程。现在,您只需要知道服务器和客户端存根都有一个SayHello()
RPC方法,该方法HelloRequest
从客户端获取 参数并HelloReply
从服务器返回a ,并且该方法的定义如下:
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
打开helloworld/helloworld.proto
并添加SayHelloAgain()
具有相同请求和响应类型的新方法:
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
// Sends another greeting
rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
记住要保存文件!
在使用新的服务方法之前,您需要重新编译更新的 .proto
文件。
仍在examples/helloworld
目录中时,运行以下命令:
$ protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \ helloworld/helloworld.proto
这将重新生成helloworld/helloworld.pb.go
和helloworld/helloworld_grpc.pb.go
文件,其中包含:
去,Go的三个最新主要 发行版中的任何一个。
有关安装说明,请参阅Go入门。 指导。
协议缓冲区编译器,protoc
,第3版。
有关安装说明,请参阅“协议缓冲区编译器安装”。
用于协议编译器的Go插件:
使用以下命令为Go安装协议编译器插件:
$ export GO111MODULE=on # Enable module mode
$ go get google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc
更新您的,PATH
以便protoc
编译器可以找到插件:
$ export PATH="$PATH:$(go env GOPATH)/bin"
将回购文件下载为zip文件 并解压缩,或克隆存储库:
$ git clone -b v1.35.0 https://github.com/grpc/grpc-go
转到快速入门示例目录:
$ cd grpc-go/examples/helloworld
编译并执行服务器代码:
$ go run greeter_server/main.go
在另一个终端上,编译并执行客户端代码以查看客户端输出:
$ go run greeter_client/main.go
Greeting: Hello world