Golang grpc setup

19 May, 2022

Protocol Buffers and GRPC in Go

Setup Compiling Protocol Buffer with protoc

To transfer data between the systems, we will make use of the compiled files from the proto proto files. We will make use of the structs (gotten from the compiled files) to create the binary data.

Steps we will follow:

  1. Install the protoc comman-line tool and the proto library
  2. Write .proto file
  3. Compile the file for Go target language
  4. Import the structs from the generated file and create data using those
  5. Serialize the data into binary format and send it to the other system
  6. On the remote machine, de-serialize the data and consume it

We will install he protoc command line from https://github.com/protocolbuffers/protobuf/releases

For MAC and Linux

On mac

$ brew install protobuf

On Ubuntu or Linux, we can copy protoc to the /usr/bin folder:

Make sure you grab the latest version

curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protobuf-all-3.17.3.zip


unzip protoc-3.11.3-linux-x86_64.zip -d protoc3

Move only protoc* to /usr/bin/

sudo mv protoc3/bin/protoc /usr/bin/protoc

After installing and setting up the command line tool, make sure you are able to access it from your terminal:

$ protoc --version
libprotoc 3.17.3

Referance link Protocol Buffers and GRPC in Go - DEV Community

GO local setup

go local needed to run protoc by adding the following lines to .bashrc ref link: protoc-gen-go: program not found or is not executable · Issue #795 · golang/protobuf · GitHub

export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin

Now in our project directory, create a new folder protofiles and then in it let us create a proto file representing/modeling information:


syntax = "proto3";

package protofiles;
option go_package = "./chat";

message Message {
  string body = 1;

service ChatService {
  rpc SayHello(Message) returns (Message) {}

The second line package protofiles is the package name for go to compile.

To compile our chat.proto proto-buf file, cd to the protofiles directory and run:

  1. go get -u github.com/golang/protobuf/protoc-gen-go
    protoc-gen-go is a plugin for the Google protocol buffer compiler to generate Go code.

  2. protoc --go_out=plugins=grpc:chat ./protofiles/chat.proto
    this will create a Go target file in the present working directory from where the command is run (the dot) and make use of all the proto files to do so

This will generate a chat.pb.go file. If you open this file you will see that it contains the auto generated code for us.
This will have multiple getter and setter methods for the structs to get and set the values.