Application auto build versioning - go

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 autoincrementing section:
$ myapp -version
MyApp version 0.5.132
Being 0.5 the version number I set, and 132 a value that increments automatically each time the binary is compiled.
Is this possible in Go?

The Go linker (go tool link) has an option to set the value of an uninitialised string variable:
-X importpath.name=value
Set the value of the string variable in importpath named name to
value.
Note that before Go 1.5 this option took two separate arguments.
Now it takes one argument split on the first = sign.
As part of your build process, you could set a version string variable using this. You can pass this through the go tool using -ldflags. For example, given the following source file:
package main
import "fmt"
var xyz string
func main() {
fmt.Println(xyz)
}
Then:
$ go run -ldflags "-X main.xyz=abc" main.go
abc
In order to set main.minversion to the build date and time when building:
go build -ldflags "-X main.minversion=`date -u +.%Y%m%d.%H%M%S`" service.go
If you compile without initializing main.minversion in this way, it will contain the empty string.

Use 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
Use 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 <path_to_binary> | 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

Additionally I would like to post a small example how to use git and a makefile:
--- Makefile ----
# This how we want to name the binary output
BINARY=gomake
# These are the values we want to pass for VERSION and BUILD
# git tag 1.0.1
# git commit -am "One more change after the tags"
VERSION=`git describe --tags`
BUILD=`date +%FT%T%z`
# Setup the -ldflags option for go build here, interpolate the variable values
LDFLAGS_f1=-ldflags "-w -s -X main.Version=${VERSION} -X main.Build=${BUILD} -X main.Entry=f1"
LDFLAGS_f2=-ldflags "-w -s -X main.Version=${VERSION} -X main.Build=${BUILD} -X main.Entry=f2"
# Builds the project
build:
go build ${LDFLAGS_f1} -o ${BINARY}_f1
go build ${LDFLAGS_f2} -o ${BINARY}_f2
# Installs our project: copies binaries
install:
go install ${LDFLAGS_f1}
# Cleans our project: deletes binaries
clean:
if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi
.PHONY: clean install
The make file will create two executables. One is executing function one, the other will take function two as main entry:
package main
import (
"fmt"
)
var (
Version string
Build string
Entry string
funcs = map[string]func() {
"f1":functionOne,"f2":functionTwo,
}
)
func functionOne() {
fmt.Println("This is function one")
}
func functionTwo() {
fmt.Println("This is function two")
}
func main() {
fmt.Println("Version: ", Version)
fmt.Println("Build Time: ", Build)
funcs[Entry]()
}
Then just run:
make
You will get:
mab#h2470988:~/projects/go/gomake/3/gomake$ ls -al
total 2020
drwxrwxr-x 3 mab mab 4096 Sep 7 22:41 .
drwxrwxr-x 3 mab mab 4096 Aug 16 10:00 ..
drwxrwxr-x 8 mab mab 4096 Aug 17 16:40 .git
-rwxrwxr-x 1 mab mab 1023488 Sep 7 22:41 gomake_f1
-rwxrwxr-x 1 mab mab 1023488 Sep 7 22:41 gomake_f2
-rw-rw-r-- 1 mab mab 399 Aug 16 10:21 main.go
-rw-rw-r-- 1 mab mab 810 Sep 7 22:41 Makefile
mab#h2470988:~/projects/go/gomake/3/gomake$ ./gomake_f1
Version: 1.0.1-1-gfb51187
Build Time: 2016-09-07T22:41:38+0200
This is function one
mab#h2470988:~/projects/go/gomake/3/gomake$ ./gomake_f2
Version: 1.0.1-1-gfb51187
Build Time: 2016-09-07T22:41:39+0200
This is function two

I had trouble using the -ldflags parameter when building my mixed command-line app and library project, so I ended up using a Makefile target to generate a Go source file containing my app's version and the build date:
BUILD_DATE := `date +%Y-%m-%d\ %H:%M`
VERSIONFILE := cmd/myapp/version.go
gensrc:
rm -f $(VERSIONFILE)
#echo "package main" > $(VERSIONFILE)
#echo "const (" >> $(VERSIONFILE)
#echo " VERSION = \"1.0\"" >> $(VERSIONFILE)
#echo " BUILD_DATE = \"$(BUILD_DATE)\"" >> $(VERSIONFILE)
#echo ")" >> $(VERSIONFILE)
In my init() method, I do this:
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s version %s\n", os.Args[0], VERSION)
fmt.Fprintf(os.Stderr, "built %s\n", BUILD_DATE)
fmt.Fprintln(os.Stderr, "usage:")
flag.PrintDefaults()
}
If you wanted an atomically-increasing build number instead of a build date, however, you would probably need to create a local file that contained the last build number. Your Makefile would read the file contents into a variable, increment it, insert it in the version.go file instead of the date, and write the new build number back to the file.

On Windows OS given the program below
package main
import "fmt"
var (
version string
date string
)
func main() {
fmt.Printf("version=%s, date=%s", version, date)
}
You can build using
go build -ldflags "-X main.version=0.0.1 -X main.date=%date:~10,4%-%date:~4,2%-%date:~7,2%T%time:~0,2%:%time:~3,2%:%time:~6,2%"
Date format assumes your environment echo %date% is Fri 07/22/2016 and echo %time% is 16:21:52.88
Then the output will be: version=0.0.1, date=2016-07-22T16:21:52

to use multi -ldflags:
$ go build -ldflags "-X name1=value1 -X name2=value2" -o path/to/output

Building on the other answers, with recent go versions it's also possible to write a buildid to an ELF section - though that's not so easily readable from within the program.
I write the same value to both, using something like the following:
BuildInfo:= "BUILD #x, branch # rev built yymmdd hh:mm:ss"
// note the nested quotes "''" required to get a string with
// spaces passed correctly to the underlying tool
ldFl := fmt.Sprintf("-X 'main.buildId=%s' -s -w '-buildid=%s'", BuildInfo, BuildInfo)
args := []string{
"build",
"-ldflags", ldFl,
"-trimpath",
"-gcflags", "-dwarf=false",
}
buildpath:="path/to/my/cmd"
args=append(args,buildpath)
buildCmd:=exec.Command("go", args...)
I use this with mage, a build tool written in go. You don't need the extra flags above, but I chose those to strip as much information as possible from release binaries.
(off topic: Mage requires a bit more upfront work than something like Make, but is much easier to extend/maintain than a make-based build system - plus you don't have to switch mental gears between go and some other syntax.)

Related

With "go list" how to list only Go modules used in the binary?

I want to list the modules (and their versions) that are compiled in the final executable (and not other dependencies).
I can do that with:
$ go build -o a.out
$ go version -m a.out
But how can I do that with go list (which has a convenient JSON output)?
I tried this:
$ go list -m -f '{{define "M"}}{{.Path}}#{{.Version}}{{end}}{{if not .Main}}{{if .Replace}}{{template "M" .Replace}}{{else}}{{template "M" .}}{{end}}{{end}}' all
But it lists many transitive dependencies which are only used in test suites for example.
I don't see how I could filter out those dependencies.
Here is a sample project to see the problem (available on The Go Playground):
main.go:
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
main_test.go:
package main
import (
"github.com/google/go-cmp/cmp"
"testing"
)
func TestHelloWorld(t *testing.T) {
if !cmp.Equal(1, 1) {
t.Fatal("FAIL")
}
}
go.mod:
module play.ground
go 1.15
require github.com/google/go-cmp v0.5.2
$ go build -o hello ; go version -m hello
hello: go1.15
path play.ground
mod play.ground (devel)
$ go list -m -f '{{define "M"}}{{.Path}}#{{.Version}}{{end}}{{if not .Main}}{{if .Replace}}{{template "M" .Replace}}{{else}}{{template "M" .}}{{end}}{{end}}' all
github.com/google/go-cmp#v0.5.2
golang.org/x/xerrors#v0.0.0-20191204190536-9bdfabe68543
Here is the answer:
go list -deps -f '{{define "M"}}{{.Path}}#{{.Version}}{{end}}{{with .Module}}{{if not .Main}}{{if .Replace}}{{template "M" .Replace}}{{else}}{{template "M" .}}{{end}}{{end}}{{end}}' | sort -u
Note: go list -deps produces one row for each package. So modules from which multiple packages are imported are listed mutiple times. sort -u sorts and removes duplicates.
It can be compared with:
go version -m hello | perl -ne 's/^\tdep\t([^\t]*)\t([^\t]*).*$/$1\#$2/ && print' | sort
Here is a version with more details that lists each package referenced from each module (also using jq:
go list -deps -f '{{define "mod"}}{{.Path}}#{{.Version}}{{end}}{{if .Module}}{{if not .Module.Main}}{{if .Module.Replace}}{{template "mod" .Module.Replace}}{{else}}{{template "mod" .Module}}{{end}}{{"\t"}}{{.ImportPath}}{{end}}{{end}}' | sort
go list -deps -json | jq -r 'select(.Module and (.Module.Main | not)) | .Module.Path + "#" + .Module.Version + "\t" + .ImportPath' | sort

Why `build` with ldflag `-X` works only when the filename is specified?

I have the file playground.go:
package main
import (
"fmt"
)
var GitCommit string
func main() {
fmt.Printf("Hello world, version: %s\n", GitCommit)
}
why the behavior of the build ldflag -X depends on specifying the source filename?:
# CASE 1
$ go build -ldflags "-X main.GitCommit=abc" && ./go_playground
Hello world, version:
$ go tool nm go_playground
52a900 D main.GitCommit
# CASE 2
$ go build -ldflags "-X main.GitCommit=abc" playground.go && ./playground
Hello world, version: abc
$ go tool nm playground
5242f0 D main.GitCommit
4c5678 R main.GitCommit.str
EDIT: it seems that the problem happens when case 1 is executed from a symlinked directory; when case 1 is executed from the original directory, the variable is passed. I'm not sure if this is expected, or it's a bug.

How to find number of flags coming from console in Golang

I'm passing argument from console. There are some flags too. Like:
go run test.go "-IP=10.10.10.10" "-db=kite" "-wv=45" "-cv=75" "A = value1" "B = value2" "C = 100" "D := ((A-B)/A)*C" "D ?"
Here, -IP, -db, -wv, -wc these four are flags and others are passing as normal argument as I know.
Number of flags can be variable.
How can I know how many flags are passed to my program. In this case 4 flags are passed.
If you use the standard flag package to parse command-line flags, you can call the NFlag function to get the number of flags:
package main
import "fmt"
import "flag"
func main() {
flag.Bool("a", true, "A value");
flag.Bool("b", true, "B value");
flag.Parse();
fmt.Println(flag.NFlag())
}
Test:
$ go run test.go
0
$ go run test.go -a
1
$ go run test.go -a -b
2

Passing go code directly into go run without a file

Is it possible to pass a string of go code into go run instead of go run /some/path/script.go? I tried:
echo "some awesome go code here" | go run
But does not work. Thanks.
I don't think that there is such an option. At least not with the standard *g compilers or
go run.
You can try using gccgo as GCC supports reading from stdin.
Since I thought that this would be a useful thing to have, I wrote a relatively small Python script that achieves what I think you want. I called it go-script, and here are some usage examples:
# Assuming that test.go is a valid go file including package and imports
$ go-script --no-package < test.go
# Runs code from stdin, importing 'fmt' and wrapping it in a func main(){}
$ echo 'fmt.Println("test")' | go-script --import fmt --main
$ echo 'fmt.Println("test")' | go-script -ifmt -m
Help:
Usage: go-script [options]
Options:
-h, --help show this help message and exit
-i PACKAGE, --import=PACKAGE
Import package of given name
-p, --no-package Don't specify 'package main' (enabled by default)
-m, --main Wrap input in a func main() {} block
-d, --debug Print the generated Go code instead of running it.
The source (also available as a gist):
#!/usr/bin/env python
from __future__ import print_function
from optparse import OptionParser
import os
import sys
parser = OptionParser()
parser.add_option("-i", "--import", dest="imports", action="append", default=[],
help="Import package of given name", metavar="PACKAGE")
parser.add_option("-p", "--no-package", dest="package", action="store_false", default=True,
help="Don't specify 'package main' (enabled by default)")
parser.add_option("-m", "--main", dest="main", action="store_true", default=False,
help="Wrap input in a func main() {} block")
parser.add_option("-d", "--debug", dest="debug", action="store_true", default=False,
help="Print the generated Go code instead of running it.")
(options, args) = parser.parse_args()
stdin = ""
for line in sys.stdin.readlines():
stdin += "%s\n" % line
out = ""
if options.package:
out += "package main\n\n"
for package in options.imports:
out += "import \"%s\"\n" % package
out += "\n"
if options.main:
out += "func main() {\n%s\n}\n" % stdin
else:
out += stdin
if options.debug:
print(out)
else:
tmpfile = "%s%s" % (os.environ["TMPDIR"], "script.go")
f = open(tmpfile, 'w')
print(out, file=f)
f.close()
os.execlp("go", "", "run", tmpfile)
This works
cat <<EOF | tee /tmp/blah.go | go run /tmp/blah.go
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
EOF
If you want to not have to open a file and edit it first. Although I wouldn't find this super practical for every day use.

package code.google.com/p/go.example/hello: exec: "hg": executable file not found in %PATH%. How to get remote golang packages?

I do as written an the Golang tutorial http://golang.org/doc/code.html#remote
My env settings:
C:\sbox\go\example>set go
GOPATH=C:\sbox\go\example
GOROOT=C:\Go
The example/ folder has only src/ folder:
C:\sbox\go\example\
|
--src\
Now I call go get as described and get an error:
C:\sbox\go\example>go get code.google.com/p/go.example/hello
# cd .; hg clone -U https://code.google.com/p/go.example C:\sbox\go\example\src\code.google.com\p\go.example
package code.google.com/p/go.example/hello: exec: "hg": executable file not found in %PATH%
After calling go get, though, my example/ folder becomes like this:
C:\sbox\go\example\
|
--src\
|
code.google.com\
|
--p\
And that's all. Nothing more installed.
Then I add a code to my directory structure and it becomes like this:
C:\sbox\go\example\
|
--src\
|
---code.google.com\
| |
| --p\
|
---github.com\
|
--user\
|
--hello\
| |
| --hello.go
|
--newmath\
|
--sqrt.go
hello.go is like this:
package main
import (
"fmt"
"github.com/user/newmath"
//"code.google.com/p/go.example/newmath"
)
func main() {
fmt.Printf("Hello, world. Sqrt(2) = %v\n", newmath.Sqrt(2))
}
sqrt.go is like this:
// Package newmath is a trivial example package.
package newmath
// Sqrt returns an approximation to the square root of x.
func Sqrt(x float64) float64 {
z := 0.0
for i := 0; i < 1000; i++ {
z -= (z*z - x) / (2 * x)
}
return z
}
I just cope/paste them. All as written in the tutorial. Then I do go install and run the project. All works fine:
C:\sbox\go\example\src\github.com\user\hello>go install
C:\sbox\go\example\bin>hello
Hello, world. Sqrt(2) = 1.414213562373095
Now I again run go get and get the same error:
C:\sbox\go\example>go get code.google.com/p/go.example/hello
# cd .; hg clone -U https://code.google.com/p/go.example C:\sbox\go\example\src\code.google.com\p\go.example
package code.google.com/p/go.example/hello: exec: "hg": executable file not found in %PATH%
Ok, I add bin/ directory to the PATH and run go get again but get the same error:
C:\sbox\go\example>set PATH=%PATH%;C:\sbox\go\example\bin
C:\sbox\go\example>go get code.google.com/p/go.example/hello
# cd .; hg clone -U https://code.google.com/p/go.example C:\sbox\go\example\src\code.google.com\p\go.example
package code.google.com/p/go.example/hello: exec: "hg": executable file not found in %PATH%
What do I need to do get the result as described in the tutorial - remote packages are installed and I can use them?
The package you are trying to install is under the Mercurial (hg) source control system. You need to install Mercurial to be able to clone the package.

Resources