|home| |posts| |projects| |cv| |bookmarks| |github|

Version Your Go Binaries

EDIT: See Version Info in Go Binaries for a better way to add version information to your Go binaries. This post is more about the embed package than about adding version info to binaries.

Go 1.16 introduced the embed package which allows you to include the contents of arbitrary files and directories in your Go application.

This feature makes it easier to version your Go binaries.

Here's a simple Go program that reads the text contained in the file version.txt(which contains the version information: e.g. v1.0) embeds it into the resulted binary and prints it when that binary file is executed with the flag -v present:

package main

import (
	_ "embed"
	"flag"
	"fmt"
)

//go:embed version.txt
var version string

func main() {
	var printVersion bool
	flag.BoolVar(&printVersion, "v", false, "print version")
	flag.Parse()
	if printVersion {
		fmt.Println(version)
	}
}

With this feature, when you want to release a new version of your program, you just have to set the version information in the version file and compile your code.