英文:
Golang compile environment variable into binary
问题
如果我编译这个程序:
package main
import (
"fmt"
"os"
)
var version = os.Getenv("VERSION")
func main() {
fmt.Println(version)
}
当我运行它时,它会打印环境变量:
VERSION="0.123" ./example
> 0.123
有没有一种方法可以将环境变量编译到二进制文件中,例如:
VERSION="0.123" go build example.go
然后在运行时获得相同的输出:
./example
英文:
If I compile this program
package main
import (
"fmt"
"os"
)
var version = os.Getenv("VERSION")
func main() {
fmt.Println(version)
}
It prints the env var when I run it
VERSION="0.123" ./example
> 0.123
Is there a way to compile the env var into the binary, for example:
VERSION="0.123" go build example.go
Then get the same output when I run
./example
答案1
得分: 36
Go 1.5及以上版本的编辑:
目前,语法已经发生了变化。
在Unix系统上使用:
go build -ldflags "-X main.Version=$VERSION"
在Windows系统上使用:
go build -ldflags "-X main.Version=%VERSION%"
-X
链接器标志是用来做什么的。在你的情况下,你可以这样做:
go build -ldflags "-X main.Version $VERSION"
编辑: 在Windows系统上,应该这样做:
go build -ldflags "-X main.Version %VERSION%"
英文:
Go 1.5 and above edit:
As of now, the syntax has changed.
On Unix use:
go build -ldflags "-X main.Version=$VERSION"
On Windows use:
go build -ldflags "-X main.Version=%VERSION%"
This is what -X
linker flag is for. In your case, you would do
go build -ldflags "-X main.Version $VERSION"
Edit: on Windows this would be
go build -ldflags "-X main.Version %VERSION%"
答案2
得分: 13
Ainer-G的答案引导我到了正确的地方,但它并不是一个完整的代码示例来回答这个问题。需要进行一些更改:
- 将版本声明为
var version string
- 使用
-X main.version=$VERSION
进行编译
以下是完整的代码:
package main
import (
"fmt"
)
var version string
func main() {
fmt.Println(version)
}
现在使用以下命令进行编译:
go build -ldflags "-X main.version=$VERSION"
英文:
Ainer-G's answer led me to the right place, but it wasn't a full code sample answering the question. A couple of changes were required:
- Declare the version as
var version string
- Compile with
-X main.version=$VERSION
Here's the full code:
package main
import (
"fmt"
)
var version string
func main() {
fmt.Println(version)
}
Now compile with
go build -ldflags "-X main.version=$VERSION"
答案3
得分: 4
有一个os.Setenv()
函数,你可以用它来设置环境变量。
所以你可以通过设置环境变量来启动你的应用程序,像这样:
func init() {
os.Setenv("VERSION", "0.123")
}
如果你不想手动这样做,你可以创建一个工具来读取VERSION
环境变量的当前值,并在你的包中生成一个.go
源文件,内容与上面的代码完全相同(除了使用VERSION
环境变量的当前值)。你可以通过执行go generate
命令来运行这样的代码生成。
阅读生成代码博文,了解关于go generate
的更多细节。
英文:
There is an os.Setenv()
function which you can use to set environment variables.
So you can start your application by setting the environment variable like this:
func init() {
os.Setenv("VERSION", "0.123")
}
If you don't want to do this by "hand", you could create a tool for this which would read the current value of the VERSION
environment variable and generate a .go
source file in your package containing exactly like the code above (except using the current value of the VERSION
environment variable). You can run such code generation by issuing the go generate
command.
Read the Generating code blog post for more details about go generate
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论