gpt4 book ai didi

go - 在 Go 中生成构建时间戳

转载 作者:IT王子 更新时间:2023-10-29 01:08:25 25 4
gpt4 key购买 nike

我想在 Go 程序中(在 Debian/Linux/x86-64 上使用 Go 1.11.1)用一行解释最后的 git commit 来保持构建时间戳。

在 C 程序中(FWIW 我的 bismon 项目正在做一些非常相似的事情),我会生成一些 _timestamp.c 文件,例如使用 Makefile 配方,例如:

_timestamp.c: 
date +'const char my_timestamp[]='%c';%n' > $@
(echo -n 'const char my_lastgitcommit[]="'; \
git log --format=oneline --abbrev=12 --abbrev-commit -q | head -1 \
| tr -d '\n\r\f\"\\\\' ; echo '";') >> $@

我会将我的程序 myprog 链接到如下内容:

myprog: $(MYOBJECTS) _timestamp.c
$(LINK.c) $(MYOBJECTS) _timestamp.c -o $@
$(RM) _timestamp.c

请注意,_timestamp.c 会在每个成功的链接处自动删除。当然,在某些 header 中我会声明 extern const char my_timestamp[];extern const char my_lastgitcommit[]: 我会使用例如my_timestampmy_lastgitcommit 在我的 main.c 中(并且 MYOBJECTS 包含 main.o)

看起来像go generate可以用来以类似的方式行事。我想要一个包 "timestamp" 定义两个字符串全局变量 timestamp.My_timestamptimestamp.My_gitcommit 但我不完全明白如何去做。

我尝试添加一些 timestamp/timestamp.go 文件

package timestamp

//go:generate date +'var My_timestamp = "%c"%n'

// Code generated - DO NOT EDIT.

但它并没有改变 go generate 然后 go install

当然,这些时间戳在编译时应该是常量字符串,我希望在运行 strings(1) 时找到它们。 ELF 可执行文件上的实用程序。

顺便说一句,我记得 the go command 的动机之一:

An explicit goal for Go from the beginning was to be able to build Go code using only the information found in the source itself, not needing to write a makefile or one of the many modern replacements for makefiles. If Go needed a configuration file to explain how to build your program, then Go would have failed.

所以我仍然期待一些东西可以单独进入源代码,而无需额外的构建配置。

换句话说,我想在每次构建时生成一个类似于以下内容的 Go 文件:

// generated timestamp.go file
package timestamp
var Buildtime = "Tue 30 Oct 2018 09:39:01 AM MET";
var Buildlastgitcommit = "7fde394b60bc adding timestamp.go"

Buildtime 字符串由date +%c 生成。 Buildlastgitcommit 字符串可能由类似于我的 _timestamp.c make 规则正在执行的命令生成。

我需要这些字符串是常量并内置在由 Go 构建生成的 ELF 可执行文件中(我更愿意通过 usual 命令来完成,或者没有额外的参数到 go build 或任何其他构建自动化工具,或者如果忘记了强制参数,则使用某种方法使构建失败;因此 atanayel's answer 是不够的)。所以我想要 strings(1)在可执行文件中快速查找这些字符串的实用程序。并且此类文件的生成应该在某种文件中进行配置,而不需要向构建器提供额外的参数。

我可以考虑换成其他的,Go-friendlybuild automation系统(但似乎即使使用 gb 我也无法轻松地做我想做的事:在每次构建时快速生成一些简单的 .go 文件)。但是我不明白为什么在 Go 程序中使用生成 Go 文件如此困难。生成简单代码遵循 Unix philosophy ,并且已经实践了几十年(例如,参见 goyacc 受到旧的 yacc 程序的启发)。

注意:rationale for go generate明确提到:

It is not a goal of this proposal to build a generalized build system like the Unix make(1) utility.

以后

once things are settled, the author commits the generated files to the source repository,

(这不是我的用例)

附言。我只关心 POSIX 系统;我真的不在乎我的 Go 软件是否不能在 Windows 上构建。我倾向于认为(与 go command motivation 解释的相反),在我的特殊情况下,我确实需要一些构建自动化工具。在我的 bastawigo玩具项目(GPLv3+),我正在使用make(驱动go命令)

最佳答案

您可以使用构建时标志来执行此操作。 This answer针对不同的用例对其进行了解释,但您的用例与之相同。你应该做这样的事情。

  • 在您的代码中定义一个常量,例如 buildTime
  • 为发布构建时,执行类似 go build -ldflags='-X buildTime="My build time output from a command"'
  • 现在您的变量 buildTime 是常量,等于 我从命令输出的构建时间
  • 您可以使用之前的命令找到正确的命令的构建时间输出

请参阅我链接的答案以获得更好的解释。

关于go - 在 Go 中生成构建时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53031035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com