This is a basic gRPC server and client written in Go. It is based on the gRPC Quickstart and gRPC Basics: Go tutorials.
I have implemented a simple gRPC server and client with the following functionality:
- simple RPC
- server-side streaming RPC
- client-side streaming RPC
- bidirectional streaming RPC
- Create a new directory for your project and cd into it
mkdir go-grpc
cd go-grpc
mkdir client server proto
- Installing the gRPC Go plugin
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]
export PATH="$PATH:$(go env GOPATH)/bin"
- Initialize a Go module
go mod init github.com/your_username/go-grpc
go mod tidy
-
Create the proto file with the required services and messages in the proto directory
-
Generate .pb.go files from the proto file
depending on what path you mention in your greet.proto file, you will either run this -
protoc --go_out=. --go-grpc_out=. proto/greet.proto
OR this -
protoc --go_out=. --go_opt=module=github.com/prasher1421/basic-go-grpc --go-grpc_out=. --go-grpc_opt=module=github.com/prasher1421/go-grpc proto/greet.proto
- Create the server and client directories and create the main.go files with necessary controllers and services
- Install the dependencies
go mod tidy
- Run the server
go run server/main.go
- Run the client
go run client/main.go