在一个 Golang 应用程序中,如何将版本嵌入到除 main 包之外的其他包中?

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

In a Golang application, how to embed a version in a other package than main?

问题

受到这个 Stack Overflow 问题的启发,我想使用相同的机制在我的 Golang 应用程序中嵌入一个版本号。然而,我正在使用 Cobra 命令行解析器,并且想要有一个 version 子命令。这导致了以下的目录和包结构:

.
|-- cmd
`-- version.go
|-- main.go

到目前为止,我尝试了以下方法:

go run -ldflags "-X cmd/version.versionString=0.1.0" main.go version
go run -ldflags "-X version.versionString=0.1.0" main.go version
go run -ldflags "-X version.VersionString=0.1.0" main.go version

其中 version.go 包含如下变量声明:

var versionString string

var VersionString string

我还尝试将变量声明放在 main.go 中,但是我不清楚如何在 version.go 中引用这些变量。对于这个选项,我尝试了以下方法:

import "github.com/basbossink/psiw"
....
fmt.Println(psiw.VersionString)
import "github.com/basbossink/psiw/main"
...
fmt.Println(main.VersionString)

在这两种情况下,编译器都会报错,指出 psiwmain 是未知的。请注意,在 main 中使用 VersionString 可以得到预期的结果。

我更希望找到一个解决方案,其中链接标志指向 version 包中的变量,因为它们不需要一个“反向指针”。但是欢迎任何建议。

英文:

Inspired by this SO question I wanted to use the same mechanism to embed a version number in my golang application. However I'm using the Cobra command line parser and want to have a version sub-command. This leads to the following directory and package structure:

.
|-- cmd                                                                   
`-- version.go                                                    
|-- main.go                                                           

Up to now I have tried the following:

go run -ldflags "-X cmd/version.versionString=0.1.0" main.go version

-

go run -ldflags "-X version.versionString=0.1.0" main.go version

-

go run -ldflags "-X version.VersionString=0.1.0" main.go version

With version.go containing a variable declaration like:

var versionString string

and

var VersionString string

respectively.

I've also tried to put the variable declarations in main.go but then I'm not clear how to reference the variables in version.go for this option I tried:

import "github.com/basbossink/psiw"
....
fmt.Println(psiw.VersionString)

-

import "github.com/basbossink/psiw/main"
...
fmt.Println(main.VersionString)

In both cases the compiler complains that psiw and main respectively are unknown.
Note that using the VersionString in main gives the expected result.

I would prefer a solution where the link flags point to the variable in the version package since they don't require a back-pointer. But any suggestions are welcome.

答案1

得分: 7

通过查看Hugo源代码,我找到了答案。诀窍是使用以下命令:

go run -ldflags "-X github.com/basbossink/psiw/cmd.versionString=0.1.0" main.go version

所以我犯了两个错误:

  • 我混淆了包和源文件的概念,version.go文件的包声明为package cmd
  • 引用变量的方式是使用完全限定的名称。
英文:

Looking at the Hugo source I've discovered the answer to my question. The trick is to use:

go run -ldflags "-X github.com/basbossink/psiw/cmd.versionString=0.1.0" main.go version

So I made two mistakes:

  • I mixed up the notion of package and source file, the version.go file had package cmd as it's package statement.
  • The way to reference the variable is to use the fully qualified name.

huangapple
  • 本文由 发表于 2016年2月11日 07:49:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/35328273.html
匿名

发表评论

匿名网友

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

确定