Sitemap

GoLang Hands-on with gRPC-Unary

5 min readApr 30, 2022

In case you are landing here directly, it’s strongly suggested that you go and read through this for fundamentals.

Press enter or click to view image in full size

Part #1.) :- Steps to demonstrate the basic setup :-

Step #1.) Here is the pre-work i.e. installing “vscode-proto3” plugin to our IDE. We are using Visual-Studio IDE :-

Press enter or click to view image in full size

Step #2.) Let’s proceed now to install “clang-Format” plugin to our IDE. We are using Visual-Studio IDE :-

Press enter or click to view image in full size

Step #3.) Let’s install certain perquisite :-

brew install clang-format
Press enter or click to view image in full size

Step #4.) Let’s install another important perquisite :-

brew install protobuf
Press enter or click to view image in full size

Step #4.) Let’s install grpc specific dependency :-

go get -u google.golang.org/grpc
Press enter or click to view image in full size

Step #5.) Let’s install some important plugins specific dependency :-

brew install protoc-gen-go
brew install protoc-gen-go-grpc
Press enter or click to view image in full size

Part #2.) :- We are now setting up the gRPC based server :-

Step #1.) Let’s first setup an empty project , by doing init :-

go mod init github.com/adityagoel123/grpc-aditya-learning
Press enter or click to view image in full size

Post the above step is being executed, we would be able to see a file called “go.mod” :-

Press enter or click to view image in full size

Step #2.) Next, let’s create a new folder “greet” and inside the same, let’s create three separate folders namely :-

  • client
  • server
  • proto
Press enter or click to view image in full size

Step #3.) Next, let’s create a our proto file, which would contain our Request/Response structures and Service. If you are new to protos, please read & clap my this blog.

Press enter or click to view image in full size

Step #4.) Now, as shown in terminal in aforementioned screenshot, let’s now utilise our proto-compiler and auto-generate the code now :-

protoc -Igreet/proto --go_opt=module=github.com/adityagoel123/grpc-aditya-learning --go_out=. --go-grpc_opt=module=github.com/adityagoel123/grpc-aditya-learning --go-grpc_out=. greet/proto/*.proto
Press enter or click to view image in full size

Step #5.) Next step for us is to write the code for our Server :-

  • At line #18, we are listening on the default TCP port i.e. 50051. This is also a default port at which gRPC server usually runs.
  • At line #27, we are launching a new-server through gRPC.
  • At line #28, we are registering our gRPC server with the Service, so that, it can start serving the traffic on the same.
Press enter or click to view image in full size

Step #6.) Next we execute the below command, in order to streamline all of our dependencies :-

go mod tidy
Press enter or click to view image in full size

Note that, aforementioned command also generates a new file “go.sum”, where all dependencies would be clearly stated :-

Press enter or click to view image in full size

Step #7.) Next we perform the build of our server, so as to generate executables from it :-

go build -o bin/greet/server ./greet/server
Press enter or click to view image in full size

Step #8.) Last, but not the least, we can start the server as follows :-

./bin/greet/server
Press enter or click to view image in full size

Part #3.) :- We are now setting up the gRPC based client :-

Step #1.) Now proceed for setting up the gRPC client :-

  • At line #13, we are setting-up gRPC-connection in an un-secured manner.
  • At line #19, we are indicating our intent that, gRPC-connection shall be closed, once the entire processing of main function is done with.
Press enter or click to view image in full size

Step #2.) We have now started registering the client and client have started invoking the functions @ server-side :-

Press enter or click to view image in full size

Step #3.) Next, as shown above screenshot, we shall proceed for building our client :-

go build -o bin/greet/client ./greet/client

As a result of the aforementioned command execution, executable file shall also be created :-

Press enter or click to view image in full size

Step #3.) Next, we shall proceed ahead to execute our client, which would eventually make a call to the Server’s gRPC function.

./bin/greet/client
Press enter or click to view image in full size

That’s all in this section. If you liked reading this blog, kindly do press on clap button multiple times, to indicate your appreciation. We would see you in next series.

References :-

aditya goel
aditya goel

Written by aditya goel

Software Engineer for Big Data distributed systems