The program written by Golang injects some version information, and the debugging speed is naturally several times faster!

Keywords: Programming git GitLab Linux Windows

background

Print git submission information compilation information when Go program runs Golang compiling information injection program When debug ging, we need to know what version of go program is currently running Don't waste time trying to confirm the version External parameters can be injected during go build compilation Let go program print the parameters at compile time when running

Take gitlab runner as an example

gitlab-runner -v
Version:      11.10.1
Git revision: 1f513601
Git branch:   11-10-stable
GO version:   go1.8.7
Built:        2019-04-24T09:29:18+0000
OS/Arch:      linux/amd64

The information printed by the terminal when the go program is finally implemented is as follows

App Name:       app-api
App Version:    v2.0.1
Build version:  84d4ffb verdor
Build time:     2019-08-06T09:58:48+0800
Git revision:   84d4ffb
Git branch:     master
Golang Version: go version go1.12.2 linux/amd64
2019-07-24 10:53:34.732 11516: http server started listening on [:20000]

Concrete realization

Entry file main.go

package main

import (
	"fmt"
)

var (
	AppName      string // apply name
	AppVersion   string // Application version
	BuildVersion string // Compiled version
	BuildTime    string // Compile time
	GitRevision  string // Git version
	GitBranch    string // Git bifurcation
	GoVersion    string // Golang information
)


func main() {
	Version()
	// Your business code entry
}

// Version version information
func Version() {
	fmt.Printf("App Name:\t%s\n", AppName)
	fmt.Printf("App Version:\t%s\n", AppVersion)
	fmt.Printf("Build version:\t%s\n", BuildVersion)
	fmt.Printf("Build time:\t%s\n", BuildTime)
	fmt.Printf("Git revision:\t%s\n", GitRevision)
	fmt.Printf("Git branch:\t%s\n", GitBranch)
	fmt.Printf("Golang Version: %s\n", GoVersion)
}

Build compile build script build.sh

#!/bin/bash

set -e

PROJECT_NAME="app-api"
BINARY="app-api"

OUTPUT_DIR=output
GOOS=$(go env GOOS)

APP_NAME=${PROJECT_NAME}
APP_VERSION=$(git log -1 --oneline)
BUILD_VERSION=$(git log -1 --oneline)
BUILD_TIME=$(date "+%FT%T%z")
GIT_REVISION=$(git rev-parse --short HEAD)
GIT_BRANCH=$(git name-rev --name-only HEAD)
GO_VERSION=$(go version)

CGO_ENABLED=0 go build -a -installsuffix cgo -v -mod=vendor \
-ldflags "-s -X 'main.AppName=${APP_NAME}' \
			-X 'main.AppVersion=${APP_VERSION}' \
			-X 'main.BuildVersion=${BUILD_VERSION}' \
			-X 'main.BuildTime=${BUILD_TIME}' \
			-X 'main.GitRevision=${GIT_REVISION}' \
			-X 'main.GitBranch=${GIT_BRANCH}' \
			-X 'main.GoVersion=${GO_VERSION}'" \
-o ${OUTPUT_DIR}/${BINARY} cmd/${BINARY}.go

In essence, the external parameters injected with the - ldflags parameter are added to the variables of go More help on build parameters of go can be obtained through go help build

Questions and answers

Q: The development environment is windows. What to do without bash? A: Git is installed, so Git Bash terminal is supported

> Flowing fish Release!

Posted by dcav on Sun, 12 Jan 2020 06:05:50 -0800