Last active
April 5, 2017 03:23
-
-
Save gnilchee/aabe19591084b25cfd568b513810ba06 to your computer and use it in GitHub Desktop.
Quick example on how to shell out to commandline from go
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 | |
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