package main import ( "fmt" "os/exec" ) func main() { cmd := exec.Command("ls", "-l") out, err := cmd.Output() if err != nil { fmt.Println("Error:", err) return } fmt.Println(string(out)) }
package main import ( "fmt" "os" "os/exec" ) func main() { cmd := exec.Command("echo", "$MESSAGE") cmd.Env = append(os.Environ(), "MESSAGE=Hello World") out, err := cmd.Output() if err != nil { fmt.Println("Error:", err) return } fmt.Println(string(out)) }This code sets an environment variable `MESSAGE` to `Hello World` and then executes the `echo` command with `$MESSAGE`. The result is that `Hello World` is printed to the console. In this example, we have used the `os` package to retrieve the current environment variables and `append` function to add a new environment variable to it. The `os/exec` package is part of the Go standard library.