This is the 9th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

This article describes how to generate and manage version numbers and compile times in Golang.

A, cause

The author has several copies of ancestral code, the earliest one was written in the late 1990s, the second one was written in 2012, and the latest code was written in 2016. The version number is available, but there is no version management, although it is big, but also have to see. My programs generally add version numbers and build dates and times, as well as version control for easy traceability and maintenance. For C/C++, there are two macro definitions __DATE__ and __TIME_, but Golang does not, so adding version numbers is a bit of an extra study.

Second, the train of thought

This article provides two approaches. First, the variable is assigned by the ldFlags parameter added at compile time. The other is through embedded C language functions. The latter can use __DATE__ and __TIME_, both of which are generated in the script for uniformity, and the version number is also specified in the script, where customization is easier.

Three, implementation,

Method 1: Golang variable

Preparation:

Version = "v1.0" BuildTime = ` date + '% % Y - m - H: % d % % m: % S' `Copy the code

Code:

var (
    BuildTime string
    Version string
)
​
func getVersion1() string {
    return fmt.Sprintf("  %v build: %v\n", Version, BuildTime)
}
​
Copy the code

Compile:

GO111MODULE=on go build -ldflags "-X 'dbtool/cmd.BuildTime=${BuildTime}' -X 'dbtool/cmd.Version=${Version}'" -mod vendor  -o dbtool.exe main.go || exit 1Copy the code

Method 2: Call the C function

Preparation:

Version = "v1.0" BuildTime = ` date + '% % Y - m - H: % d % % m: % S' ` GIT_VERSION = $Version "build: "$BuildTime echo "Generated" $VER_FILE "for version:" $GIT_VERSION echo "#ifndef PROJECT_VERSION_H" > $VER_FILE echo "#define PROJECT_VERSION_H" >> $VER_FILE echo "" >> $VER_FILE echo "#define VERSION_NUMBER "$GIT_VERSION"" >> $VER_FILE echo "" >> $VER_FILE echo "#endif" >> $VER_FILE echo "Job done!!"Copy the code

Code:

/* #include <stdio.h> #include "version.h" char* GetVersion() { static char buffer[64] = {0}; / / code specified in the version information snprintf (buffer, 64, "% s % s build: % s \ r \ n", "v1.0", __DATE__, __TIME__); Snprintf (buffer, 64, "%s\r\n", VERSION_NUMBER); snprintf(buffer, 64," %s\r\n", VERSION_NUMBER); return buffer; } */ import "C" The above code must precede the official sentence of Goalng. Func getVersion() string {name1 := c.goString () name := C.Gostring (name1) return fmt.sprintf (" %v",  name) }Copy the code

Compile:

GO111MODULE=on go build -mod vendor -o dbtool.exe main.go || exit 1
Copy the code

Four, test,

/dbtool.exe --version dbtool.exe version V1.0 build: 2021-11-04 23:14:58Copy the code

BuildTime builds dates using the date command, using the default form as follows:

Exe --version dbtool.exe version v1.0 build: Nov 4 2021 23:15:30Copy the code

Five, maintenance,

The version number needs to be changed in the compiled script, either manually or automatically generated based on iterations of SVN or GIT versions (see my article several years ago).

Source code warehouse

Source at github repository: github.com/latelee/cmd…