将Golang编译环境变量嵌入二进制文件中。

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

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%"

More info in the linker docs.

答案2

得分: 13

Ainer-G的答案引导我到了正确的地方,但它并不是一个完整的代码示例来回答这个问题。需要进行一些更改:

  1. 将版本声明为var version string
  2. 使用-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:

  1. Declare the version as var version string
  2. 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.

huangapple
  • 本文由 发表于 2015年2月12日 00:15:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/28459102.html
匿名

发表评论

匿名网友

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

确定