Backend Development 6 min read

Using Protobuf with Go: Installation, Code Examples, and Serialization

This article provides a step‑by‑step guide on installing the Protobuf compiler, setting up the Go plugin, writing .proto definitions, generating Go code, and demonstrating serialization and deserialization of Protobuf messages in a Go application, complete with troubleshooting tips and reference links.

JD Tech
JD Tech
JD Tech
Using Protobuf with Go: Installation, Code Examples, and Serialization

Protobuf is a Google‑developed data description language that serializes structured data, offering smaller message size, faster parsing speed, and high integration compared to JSON and XML.

The article explains how to install the protobuf compiler (protoc) on Linux, resolve common library issues, and obtain the Go plugin protoc‑gen‑go (placed in $GOPATH/bin and added to PATH ).

Installation commands are shown, for example: wget https://github.com/google/protobuf/releases/download/v3.6.1/protobuf-all-3.6.1.tar.gz , tar zxvf protobuf-all-3.6.1.tar.gz , cd protobuf-3.6.1 , ./configure , make , make install . A common error libprotoc.so.17: cannot open shared object file is addressed by setting export LD_LIBRARY_PATH=/usr/local/lib .

After installing the plugin, the guide shows how to create an example/person.proto file with message definitions, compile it using: protoc --go_out=. person.proto , which generates person.pb.go .

A complete Go program is provided that imports the generated package, creates Person and AllPerson instances, marshals them with proto.Marshal , unmarshals with proto.Unmarshal , and prints each person's name:

package main

import (
    "example"
    "log"
    "github.com/golang/protobuf/proto"
    "fmt"
)

func main() {
    p1 := example.Person{Id: proto.Int32(1), Name: proto.String("lvxin")}
    p2 := example.Person{Id: 2, Name: "gopher"}
    allP := example.AllPerson{Per: []*example.Person{&p1, &p2}}
    data, err := proto.Marshal(&allP)
    if err != nil { log.Fatalln("Marshal data error:", err) }
    var target example.AllPerson
    err = proto.Unmarshal(data, ⌖)
    if err != nil { log.Fatalln("Unmarshal data error:", err) }
    for k, v := range target.Per {
        fmt.Println("person[", k, "]:", v.Name)
    }
}

Running the program produces output such as:

person[0]: lvxin
person[1]: gopher
Process finished with exit code 0

All source code is hosted on GitHub (https://github.com/lvxin1986/protobuf-golang), and the article lists additional reference links for further reading.

backendGoSerializationgRPCProtobufProtocol Buffers
JD Tech
Written by

JD Tech

Official JD technology sharing platform. All the cutting‑edge JD tech, innovative insights, and open‑source solutions you’re looking for, all in one place.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.