Go:编译构建时 ldflags 变量注入

Go:编译构建时 ldflags 变量注入,第1张

Go:编译构建时 ldflags 变量注入
1.变量注入

Go在编译构建时,可以注入变量,例如:

package main

import "fmt"

var Version = "0.0.1"

func main() {
	fmt.Println(Version)
}

case 1:不注入变量

$ go build -o main main.go
$ ./main
0.0.1

case 2:注入变量

$ go build -ldflags "-X main.Version=0.0.9" -o main main.go
$ ./main
0.0.9

可以观察到,在编译构建可执行文件时,通过命令,注入main包的Version变量值。


2.常用形式

在编译构建时注入变量,常常用于可执行文件版本等信息的构建。

举例如下:

2.1.目录结构
$ tree project/
project/
|-- go.mod
|-- main.go
|-- Makefile
|-- path
|   `-- to
|       `-- package
|           `-- test1280.go
`-- VERSION

3 directories, 5 files
2.2.文件内容

go.mod

module gitlab.test1280.com/group/project

go 1.16

main.go

package main

import (
	"flag"
	"fmt"
	_package "gitlab.test1280.com/group/project/path/to/package"
	"os"
)

func main() {

	// show version
	var v bool
	flag.BoolVar(&v, "v", false, "Show Version")
	flag.Parse()

	if v {
		fmt.Printf("Version    : %s\n", _package.Version)
		fmt.Printf("Go Version : %s\n", _package.GoVersion)
		fmt.Printf("Built      : %s\n", _package.Built)
		fmt.Printf("Git Commit : %s\n", _package.GitCommit)
		fmt.Printf("OS/Arch    : %s\n", _package.OSArch)
		os.Exit(0)
	}

	fmt.Println("main run ...")
}

Makefile

# 可执行文件
BIN        = project

##########################################################################################
#
# 注入变量区
#
# 软件版本
VERSION    = `cat VERSION`
# 语言版本
GOVERSION  = `go env GOVERSION`
# 构建时刻
BUILT      = `date "+%F %T"`
# 哈希摘要
GITCOMMIT  = `git rev-parse HEAD`
# 架构信息
OSARCH     = `go env GOOS`/`go env GOARCH`
# 注入变量
LDFLAGS    = "\
    -X 'gitlab.test1280.com/group/project/path/to/package.Version=${VERSION}' \
    -X 'gitlab.test1280.com/group/project/path/to/package.GoVersion=${GOVERSION}' \
    -X 'gitlab.test1280.com/group/project/path/to/package.Built=${BUILT}' \
    -X 'gitlab.test1280.com/group/project/path/to/package.GitCommit=${GITCOMMIT}' \
    -X 'gitlab.test1280.com/group/project/path/to/package.OSArch=${OSARCH}' "
##########################################################################################

project:
	go build -ldflags ${LDFLAGS} -o ${BIN}

clean:
	@echo "clean ..."
	@rm -f ${BIN}
	@echo "clean OK"

.PHONY: project clean

test1280.go

package _package

// 构建时 -ldflags -X 注入初始化
var (
	Version   string
	GoVersion string
	Built     string
	GitCommit string
	OSArch    string
)

VERSION

1.2.3
2.3.版本控制

project 目录是一个 git repo:

$ git log
commit b93b2333426605484e4d03057fd3491b8b117047
Author: xxx 
Date:   Thu Mar 17 16:19:21 2022 +0800

    init repo
2.4.测试运行

通过 make 命令按照 Makefile 规则编译构建:

$ make
go build -ldflags " -X 'gitlab.test1280.com/group/project/path/to/package.Version=`cat VERSION`' -X 'gitlab.test1280.com/group/project/path/to/package.GoVersion=`go env GOVERSION`' -X 'gitlab.test1280.com/group/project/path/to/package.Built=`date "+%F %T"`' -X 'gitlab.test1280.com/group/project/path/to/package.GitCommit=`git rev-parse HEAD`' -X 'gitlab.test1280.com/group/project/path/to/package.OSArch=`go env GOOS`/`go env GOARCH`' " -o project

可执行文件 project 已生成:

$ ll
total 2076
-rw-rw-r-- 1 jiang jiang      50 Mar 17 15:23 go.mod
-rw-rw-r-- 1 jiang jiang     531 Mar 17 16:08 main.go
-rw-rw-r-- 1 jiang jiang    1085 Mar 17 16:00 Makefile
drwxrwxr-x 3 jiang jiang      16 Mar 17 15:34 path
-rwxrwxr-x 1 jiang jiang 2105590 Mar 17 16:21 project
-rw-rw-r-- 1 jiang jiang       5 Mar 17 15:42 VERSION

测试:

$ ./project -v
Version    : 1.2.3
Go Version : go1.16.2
Built      : 2022-03-17 16:21:05
Git Commit : b93b2333426605484e4d03057fd3491b8b117047
OS/Arch    : linux/amd64

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/996192.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存