Created
March 11, 2020 22:52
-
-
Save kjk/d3e5fa8e618a0cb67a1f98c39b5a20b9 to your computer and use it in GitHub Desktop.
change program's env example for https://blog.kowalczyk.info/article/wOYk/advanced-command-execution-in-go-with-osexec.html (made with https://codeeval.dev)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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