定义 gRPC的.proto
1 | syntax = "proto3"; |
Generate gRPC code
Next we need to update the gRPC code used by our application to use the new service definition. From the same examples dir as above ($GOPATH/src/google.golang.org/grpc/examples/helloworld
)
1 | $ protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld |
一个官方的 Demo
client.go
1 | // Package main implements a client for Greeter service. |
server.go
1 | //go:generate protoc -I ../helloworld --go_out=plugins=grpc:../helloworld ../helloworld/helloworld.proto |
api.go
1 | syntax = "proto3"; |
执行 protoc -I protobufsw/ protobufsw/api.proto --go_out=plugins=grpc:protobufsw
以将 api.go 生成 api.pb.go。此后,就可以运行 server.go 和 client.go