Warm tip: This article is reproduced from serverfault.com, please click

How to set environment variables that last

发布于 2014-07-24 16:11:25

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)
    }

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.

Questioner
hey
Viewed
0
creack 2014-07-25 00:20:40

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.

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.