Is it possible to increment a minor version number automatically each time a Go app is compiled?
I would like to set a version number inside my program, with an auto
ldflags
to set variables in main
package:With file main.go
:
package main
import "fmt"
var (
version string
build string
)
func main() {
fmt.Println("version=", version)
fmt.Println("build=", build)
}
Then run:
go run \
-ldflags "-X main.version=1.0.0 -X main.build=12082019" \
main.go
Build:
go build -o mybinary \
-ldflags "-X main.version=1.0.0 -X 'main.build=$(date)'" \
main.go
ldflags
to set variable in a non-main
package:With file config.go
:
package config
import "fmt"
var (
Version string
)
func LogVersion() {
fmt.Println("version=", Version)
}
You will also need file main.go
:
package main
import (
"fmt"
"github.com/user/repo/config"
}
func main() {
config.LogVersion()
}
Build your binary first:
go build -o mybinary main.go
Find the full path of variable name you want to set:
go tool nm | grep Version
Run and build the binary again but with the ldflags
:
go run \
-ldflags "-X github.com/user/repo/config.Version=1.0.0" \
main.go --version
go build -o mybinary \
-ldflags "-X github.com/user/repo/config.Version=1.0.0" \
main.go
Inspired by https://github.com/golang/go/wiki/GcToolchainTricks#including-build-information-in-the-executable
Also if you are using goreleaser
then read this https://goreleaser.com/environment/#using-the-mainversion :
Default wise GoReleaser sets three ldflags:
main.version: Current Git tag
main.commit: Current git commit SHA
main.date: Date according RFC3339
If you want to see this in action: https://github.com/hoto/fuzzy-repo-finder/blob/master/pkg/config/config.go