在Go语言中,有没有一种方法可以在任何导入之前设置环境变量?

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

Is there a way to set an environment variable before any import in Go?

问题

我目前正在测试Go-SDL2库,只是为了好玩。我把二进制文件给了我的一个朋友,但他的机器上没有安装SDL。所以我想做的就是将这4个.so库与二进制文件一起分发,以便它可以在其他Linux机器上正常工作。这其实很容易,我只需要将LD_LIBRARY_PATH设置为当前文件夹即可。这只是为了测试目的。

问题是,在我导入go-sdl2库之前,我必须设置这个环境变量**。目前我只有一个源文件(main.go)。

我该如何实现这一点?(这是否可能?)

英文:

I'm currently testing the Go-SDL2 lib, just for fun. I gave the binary to one of my friend but he doesn't have the SDL installed on his machine. So all I want to do (is dance) is to distribute the 4 .so libs with the binary so that it will work fine on other Linux machines. It's pretty easy actually, I just have to set the LD_LIBRARY_PATH to point to the current folder. This is for testing purpose.

The problem is, I have to set this environment variable before I can import the go-sdl2 lib. For now I have a single source file (main.go obviously).

How can I achieve this ? (Is it even possible ?)

答案1

得分: 1

一种选择是使用一个脚本,在调用go-sdl2之前设置LD_LIBRARY_PATH环境变量(在同一个脚本中)。

另一个更有趣的选择是使用一个Docker镜像,基于该镜像创建一个Dockerfile,在其中安装SDL和Go(就像didstopia/sdl2其Dockerfile中所示,结合一个Golang Dockerfile)。

这样,你就拥有了一个可复制的标准环境,不需要更改LD_LIBRARY_PATH。而且你可以导出该镜像,以便你的朋友进行实验。

英文:

One option is to have a script which sets the LD_LIBRARY_PATH environment variable before calling go-sdl2 (in the same script).

The other more interesting option is to use a Docker image, make a Dockerfile based on that image, and install SDL and go in it (like in didstopia/sdl2 ad its Dockerfile, combined with a Golang Dockerfile).

You then have a reproducible standard environment, where you don't need to change LD_LIBRARY_PATH. And you can export that image in order for your friend to experiment with it.

答案2

得分: 0

你可以在主函数中检查LD_LIBRARY_PATH是否已设置,如果没有设置,可以使用os.exec重新启动自己,并添加环境变量。

基本上,你想要使用os.Args作为exec的参数,同时还要将环境变量LD_LIBRARY_PATH添加进去。

使用Bash脚本肯定是一种更简单的方法。但是如果你真的想让Go应用程序来做这个,也是可以的。

你可以尝试以下代码(未经测试):

cmd := os.Command(os.Args[0], os.Args[1:]...)
cmd.Env = append(os.Environ(), "LD_LIBRARY_PATH=./wherever")
cmd.StdErr = os.StdErr // 标准错误输出,如果需要还可以设置标准输入和标准输出
err := cmd.Run() // 阻塞直到子进程完成
if err != nil {
   os.Exit(1)
}

类似这样的代码。

英文:

You could, in main, check to see if LD_LIBRARY_PATH is set, and if not, relaunch (using: os.exec)yourself adding the environment variable.

you basically want to use os.Args as your arguments to exec, but also take your environment and add in LD_LIBRARY_PATH

Bash script is definitely a less funky way to do it. But if you really want the go app to do this, you can.

You want something like: (untested)

cmd := os.Command(os.Args[0],os.Args[1:]...)
cmd.Env = append(os.Environ, "LD_LIBRARY_PATH=./wherever")
cmd.StdErr = os.StdErr // repeat for StdIn/StdOut
err := cmd.Run() //blocks until sub process is complete
if err != nil {
   os.Exit(1)
}

Something like that
1: https://golang.org/pkg/os/exec/

huangapple
  • 本文由 发表于 2015年6月23日 16:45:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/30997967.html
匿名

发表评论

匿名网友

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

确定