Last active
December 18, 2023 18:17
-
-
Save lee8oi/a8a90f559fe48355f800 to your computer and use it in GitHub Desktop.
Running system commands interactively in Go using os/exec
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 ( | |
"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