Skip to content

Instantly share code, notes, and snippets.

@stevedoyle
Created May 9, 2024 13:42
Show Gist options
  • Save stevedoyle/04caf5a5d5565767e45afe846d27e9ab to your computer and use it in GitHub Desktop.
Save stevedoyle/04caf5a5d5565767e45afe846d27e9ab to your computer and use it in GitHub Desktop.
Execute a shell command from a Golang program
package main
import (
"fmt"
"os/exec"
)
func main() {
app := "echo"
arg0 := "-e"
arg1 := "Hello world"
arg2 := "\n\tfrom"
arg3 := "golang"
cmd := exec.Command(app, arg0, arg1, arg2, arg3)
stdout, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(string(stdout))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment