How to Go output version information in the project?

We often when using CLI tools, will have this output parameter: `` `➜ ~ docker version Client: Docker Engine - Community Version: 18.09.2 API version: 1.39 Go version: go1.10.8 Git commit: 6247962 Built: Sun Feb 10 04:12:39 2019 OS / Arch: darwin / amd64 Experimental: false ➜ ~ `` `You can print out the building when the corresponding version information, such as version, Go version, Git Commit, etc., this is how to achieve it ? # Implementation is mainly achieved when the assignment of variables constructed by `ldflags` parameters. A code such as the following: `` `package main import (" flag "" fmt "" os ") // need to assign a variable var version =" "// parameter setting -version var printVersion bool func init packet by flag () { flag.BoolVar (& printVersion, "version", false, "print program build version") flag.Parse ()} func main () {if printVersion {println (version) os.Exit (0)} fmt.Printf ( "example for print version ")}` `` build commands: `` `go build -ldflags" -X main.version = v0.1 "-o example` `` program output: `` `➜.

Guess you like

Origin www.cnblogs.com/silenceper/p/12173651.html