Skip to content

Add version info in your Go project

Here is a simple example to add version info in your go project.

homepage-banner

Introduction

Versioning is an essential part of software development. It helps developers keep track of changes made to a project and enables users to know which version of the software they are using. In this blog post, we will discuss how to add version info into your Go project.

Using Git Tags

One of the easiest ways to add version info to your Go project is by using git tags. Git tags are references to specific points in Git history. By tagging a commit with a version number, you can easily keep track of changes made to your project. To tag a commit, use the following command:

git tag v1.0.0

This command will create a tag named “v1.0.0” at the current commit. You can then use this tag to build a binary with version info embedded in it.

Using Go ldflags

Code

package main

import (
    "flag"
    "fmt"
    "os"
)

var AppVersion string = "dev"

func main() {

    Version := flag.Bool("v", false, "")
    if *Version {
        fmt.Println(AppVersion)
        os.Exit(0)
    }
}

Compile

go build -ldflags="-X 'main.AppVersion=v1.0.0'"

Test

./your-binary -v
Leave a message