This repository demonstrates how to use Protocol Buffers (.proto files) and gRPC in Go, including support for
- Unary API
- Server Streaming API
- Client Streaming API
- Bi-Directional Streaming RPC API
Ensure the following tools are installed:
Install protoc using Homebrew (macOS):
brew install protocVerify the installation:
protoc --versiongit clone https://github.com/Prayag2003/grpc-proto-go.git
cd grpc-proto-gogo mod tidyGenerate the Go bindings from the proto file:
protoc --go_out=. --go-grpc_out=. proto/greet.protoThis creates:
proto/greet.pb.goproto/greet_grpc.pb.go
cd server
go run *.goOpen a separate terminal:
cd client
go run *.gogrpc-proto-go/
├── assets/
│ └── response.png
├── client/
│ ├── bi_stream.go
│ ├── client_stream.go
│ ├── main.go
│ ├── server_stream.go
│ └── unary.go
├── proto/
│ ├── greet_grpc.pb.go
│ ├── greet.pb.go
│ └── greet.proto
├── server/
│ ├── bi_stream.go
│ ├── client_stream.go
│ ├── main.go
│ ├── server_stream.go
│ └── unary.go
├── go.mod
├── go.sum
└── README.md
syntax = "proto3";
option go_package = "./proto";
package greet_service;
service GreetService {
rpc SayHello(NoParam) returns (HelloResponse);
rpc SayHelloFromServerStreaming(NamesList) returns (stream HelloResponse);
rpc SayHelloFromClientStreaming(stream HelloRequest) returns (MessagesList);
rpc SayHelloBidirectionalStreaming(stream HelloRequest) returns (stream HelloResponse);
}
message NoParam {}
message HelloRequest { string name = 1; }
message HelloResponse { string message = 1; }
message NamesList { repeated string names = 1; }
message MessagesList { repeated string messages = 1; }