Overriding Go values with ldflags

Posted on

You can override Go values with ldflags during the build.

That way, you can inject values like the current version, build date or the current commit hash.

go build \
    -ldflags "-X 'github.com/metal-stack/v.Version=$(VERSION)' \
              -X 'github.com/metal-stack/v.Revision=$(GITVERSION)' \
              -X 'github.com/metal-stack/v.GitSHA1=$(SHA)' \
              -X 'github.com/metal-stack/v.BuildDate=$(BUILDDATE)'" \

Seen in the metal-stack/v repository.