How to implement gRPC with Golang

This article will show you how to build a basic gRPC Unary server-client. Before starting with the implementation, let us briefly describe what gRPC unary is.

What is Unary?

In the difference between the other gRPC service definitions of Server streaming, Client streaming, and Bidirectional streaming, the unary could be the simplest because it just involves a request and a response.

gRPC Life cycle

As we can see in the above image the life cycle is the following:

  1. The client makes the request by sending the needed information by calling the related method to execute the action.
  2. The server takes the request and makes all the processes related to it.
  3. The server sends the response to the client.
  4. The client gets the response and the process is completed.

Golang Coding and Example

Now that we know how the Unary server-client works, is time to build a little example where we will define the server and the client using Golang.

In our example we just going to define a server that will define a method to create a greeting, from the client we going to send the country code and the name, it will help us to send a message in different languages.

The steps that we going to follow are the following:

1. Install needed dependencies.
2. Define the proto file with the needed server methods and messages
3. Implement the Golang gRPC server
4. Implement the Go gRPC client

Note: Implement the proto files, generate the needed code from the proto files, and build the server and client.

  1. Installing dependencies:
go install[email protected] install[email protected]
In the following image I’ll show you the project structure:

Project structure:

2. Create Golang Proto file