Skip to content

Instantly share code, notes, and snippets.

@gesquive
Created April 27, 2017 21:48
Show Gist options
  • Save gesquive/4315ace7864c5507e3dc6ff249edc3c6 to your computer and use it in GitHub Desktop.
Save gesquive/4315ace7864c5507e3dc6ff249edc3c6 to your computer and use it in GitHub Desktop.
Run an external command with golang
// run a command using the shell; no need to split args
// from https://stackoverflow.com/questions/6182369/exec-a-shell-command-in-go
func runcmd(cmd string, shell bool) []byte {
if shell {
out, err := exec.Command("bash", "-c", cmd).Output()
if err != nil {
log.Fatal(err)
panic("some error found")
}
return out
}
out, err := exec.Command(cmd).Output()
if err != nil {
log.Fatal(err)
}
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment