Go Build标签 – 在Windows上获取时间戳

huangapple go评论64阅读模式
英文:

Go Build tag - getting timestamp on Windows

问题

我看到了设置构建版本的以下命令:

go build -ldflags "-X main.minversion `date -u +.%Y%m%d%.H%M%S`" service.go

但是我在Windows上无法使时间戳部分工作!

我正在使用以下参数的go 1.5版本...

build -i -ldflags "-X main.SERVER_NAME=MFFP -X main.VERSION=1.0.0 -X main.BUILD_DATE=`date -u +.%Y%m%d%.H%M%S`"

错误出现在日期部分。

这个错误与系统有关吗?
有人在Windows上尝试过吗?

谢谢!

英文:

I have seen the following command to set build version

go build -ldflags "-X main.minversion `date -u +.%Y%m%d%.H%M%S`" service.go

but I cant get the timestamp part to work on Windows !

I am using go 1.5 with the following arguments...

build -i -ldflags "-X main.SERVER_NAME=MFFP -X main.VERSION=1.0.0 -X main.BUILD_DATE=`date -u +.%Y%m%d%.H%M%S`"

The error is with the date part

Is this error system related?
Anyone tried this on Windows?

Thanks!

答案1

得分: 2

在*nix系统上,使用反引号命令替换来执行命令。date是POSIX上的内置命令,`之间的任何内容都会被解释为shell命令,并被其值替换。

cmd.exe在反引号内部不执行命令替换。在Windows shell中,有一些方法可以实现相同的效果,比如使用for /F delims="",也就是Windows shell中最强大的命令,但这种方法相当繁琐。也许可以使用支持$(command)语法的PowerShell来代替?

PS C:\users\adsmith> go build -i -ldflags "-X main.SERVER_NAME=MFFP -X main.VERSION=1.0.0 -X main.BUILD_DATE=$(Get-Date -uformat .%Y%m%d%.H%M%S)"

你可能需要调整格式——我不确定在POSIX上应该是什么样子的。这将输出:

.20160107.111641
// 2016年1月7日,11:16:41
英文:

The command on *nix systems is using backtick command substitution. date is a built-in on POSIX, and anything inside `s is interpreted as a shell command and is replaced by its value.

cmd.exe doesn't do command substitution inside backticks at all, There's some ways to accomplish the same thing using for /F delims="", aka the most powerful command in Windows shell, but it's pretty hacky. Maybe use powershell instead, which supports the $(command) syntax?

PS C:\users\adsmith> go build -i -ldflags "-X main.SERVER_NAME=MFFP -X main.VERSION=1.0.0 -X main.BUILD_DATE=$(Get-Date -uformat .%Y%m%d%.H%M%S)"

You might have to play with the format -- I'm not sure what that's supposed to look like on POSIX. This outputs:

.20160107.111641
// Jan 7th 2016, 11:16:41

huangapple
  • 本文由 发表于 2016年1月8日 02:23:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/34662420.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定