Skip to content

Instantly share code, notes, and snippets.

@kjk
Created March 11, 2020 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjk/d3e5fa8e618a0cb67a1f98c39b5a20b9 to your computer and use it in GitHub Desktop.
Save kjk/d3e5fa8e618a0cb67a1f98c39b5a20b9 to your computer and use it in GitHub Desktop.
package main
// https://blog.kowalczyk.info/article/wOYk/advanced-command-execution-in-go-with-osexec.html
import (
"fmt"
"log"
"os"
"os/exec"
)
const (
envName = "MY_TEST_ENV_VARIABLE"
)
func runEnvironTest(envValue string) {
cmd := exec.Command("go", "run", "print-env-helper.go")
if envValue != "" {
newEnv := append(os.Environ(), fmt.Sprintf("%s=%s", envName, envValue))
cmd.Env = newEnv
}
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Printf("%s", out)
}
func main() {
runEnvironTest("")
runEnvironTest("test value")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment