成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Go 這樣設(shè)置版本號:我們的項(xiàng)目也可以

開發(fā) 后端
本文通過對 Go 源碼中版本信息的學(xué)習(xí)研究,掌握了優(yōu)秀開源項(xiàng)目設(shè)置版本信息的做法。最后,演示了如何在自己的項(xiàng)目中用上該技能。

 [[429395]]

大家好,我是 polarisxu。

項(xiàng)目中,特別是開源項(xiàng)目,會特別重視項(xiàng)目的版本號。有些項(xiàng)目,會把版本號寫入源碼中,每次升級都修改源碼號。不過這不是特別好的方式。本文通過學(xué)習(xí) Go 語言源碼的處理方式來掌握它,并應(yīng)用于自己的項(xiàng)目中。

本文基于 Go1.17,不同版本的實(shí)現(xiàn)細(xì)節(jié)可能有所不同

01 如何獲取版本號

在 Go 語言項(xiàng)目中,如果要獲取當(dāng)前 Go 語言版本,只需要調(diào)用 runtime.Version:

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

02 如何實(shí)現(xiàn)的

查看 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. }\ 

根據(jù)注釋提示,在 Go 倉庫源碼中,找到 src/cmd/link,這是 Go 鏈接器的實(shí)現(xiàn)。在其中的 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 獲取,如果設(shè)置了 GOEXPERIMENT(環(huán)境變量值),則用該值。

著重看 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 文件中,有一個(gè)函數(shù) 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() 得到,該函數(shù)定義在 src/cmd/dist/build.go 中:(省略了部分細(xì)節(jié))

  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(如果前面的獲取不到,則依次執(zhí)行后續(xù)獲取步驟)

  • 從 $GOROOT/VERSION 獲取,在這個(gè)文件中放了版本信息,你可以看看你的 Go 安裝目錄下這個(gè)文件的信息
  • 從 $GOROOT/VERSION.cache 獲取
  • 根據(jù) Git 倉庫生成版本信息,并且生成緩存,這樣后續(xù)直接讀取 $GOROOT/VERSION.cache 獲取

03 自己項(xiàng)目

通過前文分析,總結(jié)下 Go 版本是如何寫入 Go 源碼的:

  • 正式版本,通過項(xiàng)目根目錄的一個(gè)文件得到,比如 $GOROOT/VERSION;
  • 非正式版本,通過 Git 獲得版本信息;為了避免編譯時(shí)重復(fù)執(zhí)行 Git 相關(guān)操作,可以生成緩存;
  • 通過環(huán)境變量控制版本信息;

最后,可以通過一個(gè) API 把版本信息公開給用戶。

對于我們自己的 Go 項(xiàng)目,通過 Git 獲得版本信息,可以通過 shell 腳本實(shí)現(xiàn),最后編譯 Go 項(xiàng)目時(shí),將版本信息通過 -X 傳遞進(jìn)去。

現(xiàn)在我們通過腳本來實(shí)現(xiàn)這個(gè)功能。

項(xiàng)目代碼如下:

  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) 

現(xiàn)在寫一個(gè) shell 腳本,通過該腳本對以上代碼進(jìn)行編譯:

  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 的值是空,判斷當(dāng)前項(xiàng)目是否是 Git 項(xiàng)目。是,則獲取版本信息,格式:master-commithash;否則,版本信息設(shè)置為 unknown;
  • 通過 go build 的 ldflags 傳遞版本信息給 main.Version;

這樣項(xiàng)目中的 Version 就設(shè)置上正確的值了。

04 總結(jié)

本文通過對 Go 源碼中版本信息的學(xué)習(xí)研究,掌握了優(yōu)秀開源項(xiàng)目設(shè)置版本信息的做法。最后,演示了如何在自己的項(xiàng)目中用上該技能。

本文沒有演示環(huán)境變量,一般用的比較少。

責(zé)任編輯:武曉燕 來源: polarisxu
相關(guān)推薦

2023-01-03 08:26:56

2023-08-02 08:46:02

Go版本號規(guī)則

2014-12-15 14:02:48

iOS版本號蘋果

2010-11-08 10:07:45

Chrome

2024-07-08 13:56:12

微服務(wù)API代碼

2023-01-09 17:46:07

項(xiàng)目版本號字段

2010-02-06 13:49:08

Linux samba

2023-02-27 14:51:40

MySQL數(shù)據(jù)庫

2017-03-30 16:56:43

Windows 10Windows版本號

2015-07-22 10:09:59

Android M版本號

2013-08-22 10:28:50

.NET MVC.NETRazor

2010-06-30 16:41:02

識別SQL Serve

2009-08-04 08:36:54

Windows 7查看系統(tǒng)版本號

2009-02-12 16:31:39

Windows7貝塔版本號

2010-06-28 10:13:17

SQL Server

2021-08-11 08:32:24

Firefox英特爾LLVM

2017-02-08 14:29:04

2019-09-19 15:15:20

LinuxMint版本號

2010-07-09 13:01:50

SQL Server

2009-04-22 08:52:26

Windows 7微軟操作系統(tǒng)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 中文字幕精品一区二区三区在线 | 四虎影院在线播放 | 欧美综合国产精品久久丁香 | 91xxx在线观看 | 亚洲美女视频 | 国产精品久久久久久久久久久新郎 | 免费观看的av毛片的网站 | 日日天天 | 午夜电影在线播放 | 久久看片| 久久久久久久久久久久久九 | 中文字幕一区二区三区在线视频 | 91麻豆产精品久久久久久夏晴子 | 盗摄精品av一区二区三区 | www.日本在线播放 | 久久成人一区 | 久久综合一区二区三区 | 国产欧美日韩一区二区三区在线 | 久久综合欧美 | av黄色在线| 亚洲精品乱码久久久久v最新版 | 日韩国产中文字幕 | 欧美11一13sex性hd| 日本精品视频一区二区 | 欧美精品一区二区三区四区 在线 | 蜜桃综合在线 | 久久精品国产亚洲一区二区 | 久久精品中文字幕 | 国产亚洲精品久久情网 | 天天草天天爱 | 久久久爽爽爽美女图片 | 欧洲尺码日本国产精品 | 日本免费在线 | 成人av电影在线观看 | 亚洲一区二区成人 | 奇米久久 | 亚洲精品在线免费播放 | 午夜精品久久久久久久99黑人 | 国产高清精品一区二区三区 | 国产午夜亚洲精品不卡 | 亚洲高清av|