Implement the client in GO

gRPC is designed to work across different languages and platforms. In this section, we will write a client in GO and invoke the CalculatorServer which we have implemented in Java.

Learning GO

To learning the basic of GO, you can go through the tutorial below. https://tour.golang.org/basics/6

Initialize our GO project with GO module

We use GO module to manage our GO client code and library dependency.

  1. Create a folder goclient in our workspace to store all of our GO code:

cd ~/grpclab
mkdir goclient

2. Execute the following command to initialize a new GO module for us:

cd goclient
go mod init cse.cslab/goclient

Generate the client stub for GO by using .proto

We reuse our calculator.proto to generate the client stub for our GO client by using protobuf compiler, protoc. Execute the following commands:

mkdir calculator

#protoc [target .proto] 
#         -I [the directory of .proto] 
#         --go_out=plugins=grpc:[destination of the generated code]
protoc ../src/main/proto/calculator.proto \
        -I ../src/main/proto \
        --go_out=plugins=grpc:./calculator

You will see calculator/calculator.pb.go is generated.

Write your GO simple client

Now we write the main for our GO client:

Compile your program

Before compilation, we need to get some packages for GO:

Now we can compile our GO code into binary main:

You will see that an executable file goclient is generated under your workspace.

Test your GO Client

Now we have the CalculatorServer and goclient. We can test our programs.

Launch the CalculatorServer in the background:

Launch the goclient:

You should see the following output from goclient:

Last updated

Was this helpful?