Globally set environment variable from within golang

I want to globally set the environment variable from my golang code which can be accessed by anything on my OS. For now I have 2 options neither of them fits my situation

  1. use os.Setenv(key, value) - This only sets the variable in the context of code, once the code ends, I see the variable’s old value
  2. use penv package (source: https://github.com/badgerodon/penv) - This doesn’t work for some operating systems like OpenBSD (unix-like OS) because it uses https://github.com/mitchellh/go-ps/ API to get the processes and it does not have openBSD and netBSD support however freeBSD works

I am looking forward to your input

Thanks in advance!

You need to change your operating system then. Under Unixes changes to the environment are only possible for the current process and it’s children.

1 Like

Thanks for your reply, I am building something for different people and I can’t enforce my users to use some specific OS :slight_smile:

Then you are probably out of luck.

Also even windows does not change the environment of already started children after changing the own environment, you always need to trigger a refresh in the child.

So you need to find another way for IPC. The Environment variables are not suitable for that.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.