如何在Go项目中输出版本信息?
我们经常在使用CLI工具的时候,都会有这样的参数输出:
```
➜ ~ docker version
Client: Docker Engine - Community
Version: 18.09.2
API version: 1.39
Go version: go1.10.8
Git commit: 6247962
Built: Sun Feb 10 04:12:39 2019
OS/Arch: darwin/amd64
Experimental: false
➜ ~
```
可以打印出构建时对应的版本信息,比如 Version,Go Version,Git Commit等,这个是如何实现的呢?
# 实现
主要是通过`ldflags`参数来实现在构建的时候对变量进行赋值。
比如下面一段代码:
```
package main
import (
"flag"
"fmt"
"os"
)
//需要赋值的变量
var version = ""
//通过flag包设置-version参数
var printVersion bool
func init() {
flag.BoolVar(&printVersion, "version", false, "print program build version")
flag.Parse()
}
func main() {
if printVersion {
println(version)
os.Exit(0)
}
fmt.Printf("example for print version")
}
```
构建命令:
```
go build -ldflags "-X main.version=v0.1" -o example
```
程序输出:
```
➜ ./example
version=v0.1
```
# 参数说明
1、-ldflags build命令中用于调用接链接器的参数
```
-ldflags '[pattern=]arg list'
arguments to pass on each go tool link invocation.
```
2、-X 链接器参数,主要用于设置变量
```
-X importpath.name=value
Set the value of the string variable in importpath named name to value.
Note that before Go 1.5 this option took two separate arguments.
Now it takes one argument split on the first = sign.
```
# 一个完整的例子
这里将version包单独做了一个包存放,只需要引入即可:
```
package main
import (
"flag"
"github.com/go-demo/version"
)
//通过flag包设置-version参数
var printVersion bool
func init() {
flag.BoolVar(&printVersion, "version", false, "print program build version")
flag.Parse()
}
func main() {
if printVersion {
version.PrintVersion()
}
}
```
构建的shell如下(也可以放在Makefile中):
```
#!/bin/sh
version="v0.1"
path="github.com/go-demo/version"
flags="-X $path.Version=$version -X '$path.GoVersion=$(go version)' -X '$path.BuildTime=`date +"%Y-%m-%d %H:%m:%S"`' -X $path.GitCommit=`git rev-parse HEAD`"
go build -ldflags "$flags" -o example example-version.go
```
TIPS: 如果值内容中含有空格,可以用单引号
最终版本输出:
```
➜ sh build.sh
➜ ./example -version
Version: v0.1
Go Version: go version go1.13.1 darwin/amd64
Git Commit: a775ecd27c5e78437b605c438905e9cc888fbc1c
Build Time: 2020-01-09 19:01:51
```
完整代码:https://github.com/go-demo/version
> 本文在公众号"学点程序"首发 
关注公众号,了解更多相关技术内容!
如何在Go项目中输出版本信息?的更多相关文章
- 如何在NodeJS项目中优雅的使用ES6
如何在NodeJS项目中优雅的使用ES6 NodeJs最近的版本都开始支持ES6(ES2015)的新特性了,设置已经支持了async/await这样的更高级的特性.只是在使用的时候需要在node后面加 ...
- 如何在cocos2d项目中enable ARC
如何在cocos2d项目中enable ARC 基本思想就是不支持ARC的代码用和支持ARC的分开,通过xcode中设置编译选项,让支持和不支持ARC的代码共存. cocos2d是ios app开发中 ...
- 如何在VUE项目中添加ESLint
如何在VUE项目中添加ESLint 1. 首先在项目的根目录下 新建 .eslintrc.js文件,其配置规则可以如下:(自己小整理了一份),所有的代码如下: // https://eslint.or ...
- 如何在maven项目中使用spring
今天开始在maven项目下加入spring. 边学习边截图. 在这个过程中我新建了一个hellospring的项目.于是乎从这个项目出发开始研究如何在maven项目中使用spring.鉴于网上的学习资 ...
- 如何在Vue-cli项目中使用JTopo
1.前言 jTopo(Javascript Topology library)是一款完全基于HTML5 Canvas的关系.拓扑图形化界面开发工具包.其体积小,性能优异,由一群开发爱好者来维护.唯一感 ...
- [Laravel-Swagger]如何在 Laravel 项目中使用 Swagger
如何在 Laravel 项目中使用 Swagger http://swagger.io/getting-started/ 安装依赖 swagger-php composer require zirco ...
- 如何在mvc项目中使用apiController
文章地址:How do you route from an MVC project to an MVC ApiController in another project? 文章地址:How to Us ...
- 如何在Ionic2项目中使用第三方JavaScript库
onic的官网放出一记大招Ionic and Typings,来介绍如何在Ionic2项目中使用第三方JavaScript库. 因为在前阵子正好想用一个非常有名的第三方JS库ChartJs来实现一些东 ...
- 获取UWP配置文件中的版本信息
原文:获取UWP配置文件中的版本信息 在一般的软件中,我们都会显示当前软件的版本信息.以前作者都是在发版的时候修改一下UWP的配置文件中的版本信息和软件中的版本信息.但是每次这样很麻烦,有时间忘记修改 ...
随机推荐
- phpstorm 中背景色的调整
- 学习layui框架
Layui是一款功能齐全的前端框架,需要引入对应的CSS文件和JS文件,附属官网链接:Layui官网
- Asp.net MVC中如何获取控制器的名称
如果在代码中 当前controller.action的获取RouteData.Route.GetRouteData(this.HttpContext).Values["controller& ...
- Java日志框架——JCL
JCL,全称为"Jakarta Commons Logging",也可称为"Apache Commons Logging". 一.JCL原理 1.基本原理 JC ...
- 第一种方式:cookie的优化与购物车实例
一 Cookie 的优化 1.1 一般而言,我们设置cookie是在php中设置 例如: <?php setcookie('testKey1','hello world',0,'/'); //# ...
- fetch是什么?写一个fetch请求
fetch是web提供的一个可以获取异步资源的api,目前还没有被所有浏览器支持,它提供的api返回的是Promise对象,所以你在了解这个api前首先得了解Promise的用法. 参考链接:http ...
- 如何在很短的时间内将大量数据插入到ConcurrentHashMap(转)
将大批量数据保存到map中有两个地方的消耗将会是比较大的:第一个是扩容操作,第二个是锁资源的争夺.第一个扩容的问题,主要还是要通过配置合理的容量大小和扩容因子,尽可能减少扩容事件的发生:第二个锁资源的 ...
- 2018-2-13-win10-uwp-图标制作器
title author date CreateTime categories win10 uwp 图标制作器 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17 ...
- poj1080 - Human Gene Functions (dp)
题面 It is well known that a human gene can be considered as a sequence, consisting of four nucleotide ...
- 很多.net 程序员不知道又非常重要的 .net高级调试技巧.调试别人的dll方法内的变量
事情是这样的, 最近需要开发Orcale的数据库. 于是使用了EF 加上 Oracle.ManagedDataAccess.Client 这个Oracle.ManagedDataAccess 很好用, ...