Menu Close

Install a Specific Version of protoc-gen-go on macOS

In this article, we are going to learn how to Install a Specific Version of protoc-gen-go on macOS with example in details.

Keywords: install protoc-gen-go, specific version, macOS, Go, Protocol Buffers, guide, example

Protocol Buffers, or simply Protobuf, is a powerful language-agnostic binary serialization format developed by Google. protoc-gen-go is the Go Protocol Buffers plugin for the protoc compiler.

Prerequisites

Before we begin, ensure you have the following tools installed:

  • Go programming language
  • protoc compiler

If you don’t have these installed, follow the instructions in the respective sections below.

Installing Go

  1. Download and install Go from the official website: https://golang.org/dl/. Follow the instructions provided for macOS.

Setting up Go environment

  • Open the Terminal and set up your Go environment variables. Add the following lines to your .bash_profile, .bashrc, or .zshrc file, depending on your shell:
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
  • Save the file and restart the terminal or run source ~/.bash_profile, source ~/.bashrc, or source ~/.zshrc to apply the changes.

Installing the protoc compiler

  1. Download the appropriate version for your platform from the Protocol Buffers GitHub releases page: https://github.com/protocolbuffers/protobuf/releases.
  2. Extract the archive and move the bin/protoc executable to a directory in your PATH, such as /usr/local/bin.

Installing a Specific Version of protoc-gen-go

Follow these steps to install a specific version of protoc-gen-go on macOS:

  • In the Terminal, run the following command, replacing vX.Y.Z with the specific version you want to install:
GO111MODULE=on go get google.golang.org/protobuf/cmd/[email protected]

For example, to install version 1.27.1, you would run:

GO111MODULE=on go get google.golang.org/protobuf/cmd/[email protected]
  • Verify the installation by running protoc-gen-go --version to check that the specific version has been installed.

Example

Here’s a practical example of how to use protoc-gen-go with a specific version:

  • Create a new directory for your project:
mkdir my-protobuf-project
cd my-protobuf-project
  • Create a sample .proto file, such as sample.proto:

syntax = "proto3";

package myproject;


option go_package = "github.com/myusername/myproject/mypackage";


message Person {

  string name = 1;

  int32 age = 2;

}

  • Compile the .proto file using the protoc compiler and the installed version of protoc-gen-go:
protoc --go_out=. --go_opt=paths=source_relative sample.proto

This command generates a Go source file named sample.pb.go in the same directory

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/golang-tutorial/

References:

https://golang.org/pkg/

Posted in golang, Miscellaneous, strconv

Leave a Reply

Your email address will not be published. Required fields are marked *