Skip to content

Instantly share code, notes, and snippets.

@gnilchee
Last active April 5, 2017 03:23
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 gnilchee/aabe19591084b25cfd568b513810ba06 to your computer and use it in GitHub Desktop.
Save gnilchee/aabe19591084b25cfd568b513810ba06 to your computer and use it in GitHub Desktop.
Quick example on how to shell out to commandline from go
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
cmdName := "ps"
cmdArgs := []string{"-ef"}
cmdOut, err := exec.Command(cmdName, cmdArgs...).Output()
if err != nil {
fmt.Fprintln(os.Stderr, "There was an error running ps -ef command: ", err)
os.Exit(1)
}
output := string(cmdOut)
fmt.Println(output)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment