如何设置持久的环境变量 [英] How to set environment variables that last

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

问题描述

我正在尝试使用转到操作系统在我的计算机上设置一些环境变量

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进程,该变量就不再可用.我想知道是否可以永久设置这些变量.原因是我希望为本地和开发环境创建一个带有配置文件(数据库名称等)的安装程序"文件,以便我可以在根本不进行任何设置的情况下进行切换...只需运行一次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库: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天全站免登陆