Go 这样设置版本号:我们的项目也可以

开发 后端
本文通过对 Go 源码中版本信息的学习研究,掌握了优秀开源项目设置版本信息的做法。最后,演示了如何在自己的项目中用上该技能。

 [[429395]]

大家好,我是 polarisxu。

项目中,特别是开源项目,会特别重视项目的版本号。有些项目,会把版本号写入源码中,每次升级都修改源码号。不过这不是特别好的方式。本文通过学习 Go 语言源码的处理方式来掌握它,并应用于自己的项目中。

本文基于 Go1.17,不同版本的实现细节可能有所不同

01 如何获取版本号

在 Go 语言项目中,如果要获取当前 Go 语言版本,只需要调用 runtime.Version:

  1. package main 
  2.  
  3. import ( 
  4.  "fmt" 
  5.  "runtime" 
  6.  
  7. func main() { 
  8.  fmt.Println("Go Version:", runtime.Version()) 

02 如何实现的

查看 runtime.Version 的源码:

  1. // buildVersion is the Go tree's version string at build time
  2. // 
  3. // If any GOEXPERIMENTs are set to non-default values, it will include 
  4. // "X:<GOEXPERIMENT>"
  5. // 
  6. // This is set by the linker. 
  7. // 
  8. // This is accessed by "go version <binary>"
  9. var buildVersion string 
  10.  
  11. // Version returns the Go tree's version string. 
  12. // It is either the commit hash and date at the time of the build or
  13. // when possible, a release tag like "go1.3"
  14. func Version() string { 
  15.  return buildVersion 
  16. }\ 

根据注释提示,在 Go 仓库源码中,找到 src/cmd/link,这是 Go 链接器的实现。在其中的 internal/ld/main.go 文件找到了如下代码:

  1. buildVersion := buildcfg.Version 
  2. if goexperiment := buildcfg.GOEXPERIMENT(); goexperiment != "" { 
  3.   buildVersion += " X:" + goexperiment 
  4. addstrdata1(ctxt, "runtime.buildVersion="+buildVersion) 

buildVersion 值从 buildcfg.Version 获取,如果设置了 GOEXPERIMENT(环境变量值),则用该值。

着重看 buildcfg.Version 如何得到的:

  1. var ( 
  2.  defaultGOROOT string // set by linker 
  3.  
  4.  GOROOT   = envOr("GOROOT", defaultGOROOT) 
  5.  GOARCH   = envOr("GOARCH", defaultGOARCH) 
  6.  GOOS     = envOr("GOOS", defaultGOOS) 
  7.  GO386    = envOr("GO386", defaultGO386) 
  8.  GOARM    = goarm() 
  9.  GOMIPS   = gomips() 
  10.  GOMIPS64 = gomips64() 
  11.  GOPPC64  = goppc64() 
  12.  GOWASM   = gowasm() 
  13.  GO_LDSO  = defaultGO_LDSO 
  14.  Version  = version 

很奇怪,Version 的值,直接用 version 赋值的。但 version 的值是什么?在 src/cmd/dist/buildruntime.go 文件中,有一个函数 mkbuildcfg,用于生成 buildcfg:

  1. // mkbuildcfg writes internal/buildcfg/zbootstrap.go: 
  2. // 
  3. // package buildcfg 
  4. // 
  5. // const defaultGOROOT = <goroot> 
  6. // const defaultGO386 = <go386> 
  7. // ... 
  8. // const defaultGOOS = runtime.GOOS 
  9. // const defaultGOARCH = runtime.GOARCH 
  10. // 
  11. // The use of runtime.GOOS and runtime.GOARCH makes sure that 
  12. // a cross-compiled compiler expects to compile for its own target 
  13. // system. That is, if on a Mac you do: 
  14. // 
  15. // GOOS=linux GOARCH=ppc64 go build cmd/compile 
  16. // 
  17. // the resulting compiler will default to generating linux/ppc64 object files. 
  18. // This is more useful than having it default to generating objects for the 
  19. // original target (in this example, a Mac). 
  20. func mkbuildcfg(file string) { 
  21.  var buf bytes.Buffer 
  22.  fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n"
  23.  fmt.Fprintln(&buf) 
  24.  fmt.Fprintf(&buf, "package buildcfg\n"
  25.  fmt.Fprintln(&buf) 
  26.  fmt.Fprintf(&buf, "import \"runtime\"\n"
  27.  fmt.Fprintln(&buf) 
  28.  fmt.Fprintf(&buf, "const defaultGO386 = `%s`\n", go386) 
  29.  fmt.Fprintf(&buf, "const defaultGOARM = `%s`\n", goarm) 
  30.  fmt.Fprintf(&buf, "const defaultGOMIPS = `%s`\n", gomips) 
  31.  fmt.Fprintf(&buf, "const defaultGOMIPS64 = `%s`\n", gomips64) 
  32.  fmt.Fprintf(&buf, "const defaultGOPPC64 = `%s`\n", goppc64) 
  33.  fmt.Fprintf(&buf, "const defaultGOEXPERIMENT = `%s`\n", goexperiment) 
  34.  fmt.Fprintf(&buf, "const defaultGO_EXTLINK_ENABLED = `%s`\n", goextlinkenabled) 
  35.  fmt.Fprintf(&buf, "const defaultGO_LDSO = `%s`\n", defaultldso) 
  36.  fmt.Fprintf(&buf, "const version = `%s`\n", findgoversion()) 
  37.  fmt.Fprintf(&buf, "const defaultGOOS = runtime.GOOS\n"
  38.  fmt.Fprintf(&buf, "const defaultGOARCH = runtime.GOARCH\n"
  39.  
  40.  writefile(buf.String(), file, writeSkipSame) 

其中 version 的值是通过 findgoversion() 得到,该函数定义在 src/cmd/dist/build.go 中:(省略了部分细节)

  1. // findgoversion determines the Go version to use in the version string. 
  2. func findgoversion() string { 
  3.  // The $GOROOT/VERSION file takes priority, for distributions 
  4.  // without the source repo. 
  5.  path := pathf("%s/VERSION", goroot) 
  6.  if isfile(path) { 
  7.   ... 
  8.  } 
  9.  
  10.  // The $GOROOT/VERSION.cache file is a cache to avoid invoking 
  11.  // git every time we run this command. Unlike VERSION, it gets 
  12.  // deleted by the clean command. 
  13.  path = pathf("%s/VERSION.cache", goroot) 
  14.  if isfile(path) { 
  15.   return chomp(readfile(path)) 
  16.  } 
  17.  
  18.  // Show a nicer error message if this isn't a Git repo. 
  19.  if !isGitRepo() { 
  20.   fatalf("FAILED: not a Git repo; must put a VERSION file in $GOROOT"
  21.  } 
  22.  
  23.  // Otherwise, use Git. 
  24.  // What is the current branch? 
  25.  branch := chomp(run(goroot, CheckExit, "git""rev-parse""--abbrev-ref""HEAD")) 
  26.  ... 
  27.  
  28.  // Cache version. 
  29.  writefile(tag, path, 0) 
  30.  
  31.  return tag 

按一下顺序获得 Version(如果前面的获取不到,则依次执行后续获取步骤)

  • 从 $GOROOT/VERSION 获取,在这个文件中放了版本信息,你可以看看你的 Go 安装目录下这个文件的信息
  • 从 $GOROOT/VERSION.cache 获取
  • 根据 Git 仓库生成版本信息,并且生成缓存,这样后续直接读取 $GOROOT/VERSION.cache 获取

03 自己项目

通过前文分析,总结下 Go 版本是如何写入 Go 源码的:

  • 正式版本,通过项目根目录的一个文件得到,比如 $GOROOT/VERSION;
  • 非正式版本,通过 Git 获得版本信息;为了避免编译时重复执行 Git 相关操作,可以生成缓存;
  • 通过环境变量控制版本信息;

最后,可以通过一个 API 把版本信息公开给用户。

对于我们自己的 Go 项目,通过 Git 获得版本信息,可以通过 shell 脚本实现,最后编译 Go 项目时,将版本信息通过 -X 传递进去。

现在我们通过脚本来实现这个功能。

项目代码如下:

  1. // main.go 
  2. package main 
  3.  
  4. import ( 
  5.  "fmt" 
  6.  
  7. var Version string 
  8.  
  9. func main() { 
  10.  fmt.Println("Version:", Version) 

现在写一个 shell 脚本,通过该脚本对以上代码进行编译:

  1. #!/bin/sh 
  2.  
  3. version="" 
  4.  
  5. if [ -f "VERSION" ]; then 
  6.     version=`cat VERSION` 
  7. fi 
  8.  
  9. if [[ -z $version ]]; then 
  10.     if [ -d ".git" ]; then 
  11.         version=`git symbolic-ref HEAD | cut -b 12-`-`git rev-parse HEAD` 
  12.     else 
  13.         version="unknown" 
  14.     fi 
  15. fi 
  16.  
  17. go build -ldflags "-X main.Version=$version" main.go 
  • 如果有 VERSION 文件,读取该文件的值作为版本信息;
  • 如果 version 的值是空,判断当前项目是否是 Git 项目。是,则获取版本信息,格式:master-commithash;否则,版本信息设置为 unknown;
  • 通过 go build 的 ldflags 传递版本信息给 main.Version;

这样项目中的 Version 就设置上正确的值了。

04 总结

本文通过对 Go 源码中版本信息的学习研究,掌握了优秀开源项目设置版本信息的做法。最后,演示了如何在自己的项目中用上该技能。

本文没有演示环境变量,一般用的比较少。

责任编辑:武晓燕 来源: polarisxu
相关推荐

2023-01-03 08:26:56

2023-08-02 08:46:02

Go版本号规则

2014-12-15 14:02:48

iOS版本号苹果

2010-11-08 10:07:45

Chrome

2010-02-06 13:49:08

Linux samba

2023-01-09 17:46:07

项目版本号字段

2017-03-30 16:56:43

Windows 10Windows版本号

2015-07-22 10:09:59

Android M版本号

2023-02-27 14:51:40

MySQL数据库

2009-02-12 16:31:39

Windows7贝塔版本号

2010-06-30 16:41:02

识别SQL Serve

2009-08-04 08:36:54

Windows 7查看系统版本号

2010-06-28 10:13:17

SQL Server

2017-02-08 14:29:04

2019-09-19 15:15:20

LinuxMint版本号

2021-08-11 08:32:24

Firefox英特尔LLVM

2010-07-09 13:01:50

SQL Server

2011-03-04 13:47:15

IE9

2021-06-29 06:41:11

Windows 11操作系统微软

2013-05-16 09:36:07

Java甲骨文Java版本
点赞
收藏

51CTO技术栈公众号