Golang 编译时使用ldflags加入版本信息等

Golang 编译时使用ldflags加入版本信息等

hb0730 84 2021-11-25

概述

在做 drone插件时,发现了一个库drone-dingtalk-message 在使用 goreleaser 编译时只用了ldflags指令,于是了解了一下

编译时加入自定义信息到程序中

main路径下

通常情况下,我们直接使用go build来编译程序,要想加入自定义信息到程序中,很简单,编译时使用-ldflags就行。

例如有如下程序test.go:

package main

import "fmt"

var AAA string
var BBB string

func main() {
    fmt.Println("AAA =", AAA)
    fmt.Println("BBB =", BBB)
}

在编译时加入-ldflags:

go build -ldflags "-X 'main.AAA=111' -X 'main.BBB=222'"

运行程序:

./test

AAA = 111
BBB = 222

可以看到AAA和BBB两个变量的值变成了111和222,也就是在编译时加入的信息。

文档中可以看到相关参数的说明:

hdr-Compile_packages_and_dependencies

Command Line

-ldflags '[pattern=]arg list'
    arguments to pass on each go tool link invocation.

-X importpath.name=value
    Set the value of the string variable in importpath named name to value.
    This is only effective if the variable is declared in the source code either uninitialized
    or initialized to a constant string expression. -X will not work if the initializer makes
    a function call or refers to other variables.
    Note that before Go 1.5 this option took two separate arguments.

这里的importpath.name=value可以理解为包名.变量名,比如上面的main.AAA就表示main包下的AAA变量

非main路径

查找导入路径

对于不在 main 包下的变量,我们需要先知道其导入路径。此步骤可以通过 go tool nm 实现,完整示例如下:

go build -o pure
go tool nm pure | grep -i version

78a060 T crypto/tls.(*Config).mutualVersion
  789e80 T crypto/tls.(*Config).supportedVersions
  7986a0 T crypto/tls.(*Conn).pickTLSVersion
  7af820 T crypto/tls.prfAndHashForVersion
  daf320 D crypto/tls.supportedVersions
  dbc950 D github.com/go-git/go-git/v5/plumbing/format/idxfile.ErrUnsupportedVersion
  740840 T github.com/go-git/go-git/v5/plumbing/format/idxfile.readVersion
  d6d360 D github.com/go-git/go-git/v5/plumbing/format/index.DecodeVersionSupported
  d6d1ac D github.com/go-git/go-git/v5/plumbing/format/index.EncodeVersionSupported
  dbc9a0 D github.com/go-git/go-git/v5/plumbing/format/index.ErrUnsupportedVersion
  dba928 D github.com/go-git/go-git/v5/plumbing/format/packfile.ErrUnsupportedVersion
  5b68c0 T github.com/spf13/cobra.(*Command).InitDefaultVersionFlag
  5b20e0 T github.com/spf13/cobra.(*Command).SetVersionTemplate
  5b2e60 T github.com/spf13/cobra.(*Command).VersionTemplate
  89d620 T golang.org/x/crypto/ssh.(*Client).ClientVersion
  89dea0 T golang.org/x/crypto/ssh.(*Client).ServerVersion
  89b840 T golang.org/x/crypto/ssh.(*connection).ClientVersion
  89b940 T golang.org/x/crypto/ssh.(*connection).ServerVersion
  878da0 T golang.org/x/crypto/ssh.(*sshConn).ClientVersion
  878e60 T golang.org/x/crypto/ssh.(*sshConn).ServerVersion
  89d6c0 T golang.org/x/crypto/ssh.Client.ClientVersion
  89df40 T golang.org/x/crypto/ssh.Client.ServerVersion
  898520 T golang.org/x/crypto/ssh.exchangeVersions
  893e00 T golang.org/x/crypto/ssh.isBrokenOpenSSHVersion
  898720 T golang.org/x/crypto/ssh.readVersion
  dbb090 D golang.org/x/sys/windows.procGetVersion
  dbb338 D golang.org/x/sys/windows.procRtlGetNtVersionNumbers
  dbb340 D golang.org/x/sys/windows.procRtlGetVersion
  812200 T net/http.ParseHTTPVersion
  dbd900 D pure-admin-cli/cmd.templateVersion
  daee20 D pure-admin-cli/cmd.version
  daee40 D pure-admin-cli/constants.DefaultVersion
  daeed0 D runtime.buildVersion
  e02a00 D runtime.processorVersionInfo
  dbbd50 D syscall.procGetVersion

根据查找的信息可知,import path 为 pure-admin-cli/cmd.version 下面就可以更新此路径下的变量了

再次编译

go build -o pure -ldflags "-s -w -X pure-admin-cli/cmd.version=1.0.0-beta.0"

./pure -v
pure version 1.0.0-beta.0
  • pure-admin-cli 用于快速构建基于vue-pure-admin项目的命令行工具