This is a creation in Article, where the information may have evolved or changed.
Golang compile-time set version number
No matter what language you write the code in, the version is a very important thing. In general, it is a good idea to use the-V or--version parameters to output the version of the program. Words do not say much, on the code snippet.
package mainimport ( "flag" "fmt" "os")var VersionStr = "unknown"func main() { version := flag.Bool("v", false, "Show Version") flag.Parse() if *version { fmt.Println(VersionStr) os.Exit(0) }}
Compile script:
#!/bin/bash -xefunction getversion() { branch=`git rev-parse --abbrev-ref HEAD` commitid=`git rev-parse --short HEAD` builddate=`date +%Y%m%d-%H%M%S` echo $branch-$commitid-$builddate}cd `dirname $0`# use go vendorexport GO15VENDOREXPERIMENT=1version=`getversion`pkgpath="github.com/auxten/logrus/examples/basic"echo "build log-linux"GOOS=linux GOARCH=amd64 go build -ldflags "-X main.VersionStr=${version}" -o bin/log-linux ${pkgpath}#echo "build log-osx"#GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.VersionStr=${version}" -o bin/log-osx ${pkgpath}#echo "build log-win#GOOS=windows GOARCH=386 go build -ldflags "-X main.versionStr=${version}" -o bin/log-win ${pkgpath}echo "done"
This way, you can put the GIT branch-commitid-compile time output by running./bin/log-linux-v.
Copyright NOTICE: Reprint Please indicate article source: Reboot-51
- If the article is helpful to you, please pay attention to, click a praise!