Golang自動產生版本資訊

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

原文: Golang自動產生版本資訊

需求

golang程式在build時自動產生版本資訊,使用 ./helloworld –version可以查看版本和build時間

實現原理

使用連結選項-X設定一個二進位檔案中可以訪問的變數

  • 執行個體1:
12345678
package mainimport "fmt" var Version = "No Version Provided" func main() {    fmt.Println("HelloWorld Version is:", Version)}
123
go run -ldflags "-X main.Version 1.5" helloworld.go HelloWorld Version is: 1.5
  • 執行個體2
12345678910
package mainimport \"fmt\" var buildstamp = \"no timestamp set\"var githash = \"no githash set\" func main() {    fmt.Println(\"HelloWorld buildstamp is:\", buildstamp)    fmt.Println(\"HelloWorld buildgithash is:\", githash)}
123456
go build -ldflags \"-X main.buildstamp `date \'+%Y-%m-%d_%I:%M:%S\'` -X main.githash `git rev-parse HEAD`\" helloworld.go ./helloworld HelloWorld buildstamp is: 2015-09-08_05:58:49HelloWorld buildgithash is: 1adb00d88d832687eb4148a3871829fb73021c29

參考資料
golang-auto-build-versioning

其它一些相關的介紹:

  1. 編譯時間向 go 程式寫入 git 版本資訊
  2. command/link
  3. Setting Go variables from the outside

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.