在Go中运行os / exec命令后获取环境

I am running an external command via os.exec package. I can specify an environment for the external command using the Cmd.Env member. However, the command may modify that environment and I want to capture those modifications for subsequent invocations of the command.

How do I find out the state of the environment immediately after the command terminates?

I don't believe there is a standard interface to do what you're after in a platform independent way.

On systems like Linux for instance, the process's environment is managed through the environ global variable in the process's address space. Changes to the local environment only propagate to new processes because this environment is passed to the execve system call.

So to do what you want, you really need the cooperation of the process you are executing. Perhaps you could have it write out its environment in response to a signal?