Home > Article > Backend Development > How to Set Specific Environment Variables for exec.Command in Go?
Setting Environment Variables for exec.Command
When working with external command line tools in Go, using exec.Command allows you to execute commands and control their environment. To pass environment variables through this function, rather than setting them system-wide, you can modify the command's environment directly.
To set a specific environment variable while retaining the existing environment, follow these steps:
For example:
cmd := exec.Command("ansible-playbook", args...) cmd.Env = os.Environ() cmd.Env = append(cmd.Env, "MY_VAR=some_value")
This approach ensures that only the specified variable is modified while preserving the existing environment.
The above is the detailed content of How to Set Specific Environment Variables for exec.Command in Go?. For more information, please follow other related articles on the PHP Chinese website!