如何设置最后的环境变量?在去 [英] How to set environment variables that last ? in go

查看:139
本文介绍了如何设置最后的环境变量?在去的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Go OS 设置一些环境变量, p>

I'm trying to set some environment variables on my machine using Go OS

    err := os.Setenv("DBHOST", dbHostLocal)
    if err != nil {
        log.Fatalf("err %v", err)
    }

Go程序可用,但一旦我完成/终止Go进程,变量就不再可用了。我想知道是否可以永久设置这些变量。原因是我正在寻找用于本地和开发环境的配置文件(db name etc)创建一个安装文件,以便我可以在没有任何安装的情况下进行切换,只需一次就可以运行setup.go 。

It seems the variable is available to the Go program but once I quite / terminate the Go process the variable is no longer available. I'm wondering if it's possible to set these variables permanently. The reason is that i was looking to create a "setup" file with config files ( db name etc ) for the local and dev environment so that I could switch between without any setup at all... just one time go run setup.go.

推荐答案

短:这是不可能的。您无法更改父进程的环境。你只能改变自己,把它传给你的孩子。

Short: It is not possible. You can't change the environment of your parent process. You can only change your own and pass it to your children.

你应该做的是维护配置文件。
有很多go config libs在那里:ini,yaml等。

What you should do is maintain a config file. There are plenty of go config libs out there: ini, yaml, etc.

如果您的程序更改配置,每次更改后将其保存到磁盘,或一段时间或进程退出时。

If your program changes the config, save it to disk after each change or one in a while or when the process exits.

这篇关于如何设置最后的环境变量?在去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆