Skip to content

Instantly share code, notes, and snippets.

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