-
-
Save rafaelcalleja/1792d36a74f99fee33b363eee338e832 to your computer and use it in GitHub Desktop.
Running system commands interactively in Go using os/exec
This file contains hidden or 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 ( | |
| "os" | |
| "os/exec" | |
| ) | |
| func Command(args ...string) { | |
| cmd := exec.Command(args[0], args[1:]...) | |
| cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr | |
| if err := cmd.Run(); err != nil { | |
| panic(err) | |
| } | |
| } | |
| func main() { | |
| Command("bash") | |
| Command("ping", "-c3", "www.google.com") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment