Sometimes our Go programs need to spawn other, non-Go
processes. For example, the syntax highlighting on this
site is implemented
by spawning a |
|
![]() ![]() package main |
|
import ( "fmt" "io" "os/exec" ) |
|
func main() { |
|
We’ll start with a simple command that takes no
arguments or input and just prints something to
stdout. The |
dateCmd := exec.Command("date") |
|
dateOut, err := dateCmd.Output() if err != nil { panic(err) } fmt.Println("> date") fmt.Println(string(dateOut)) |
Next we’ll look at a slightly more involved case
where we pipe data to the external process on its
|
grepCmd := exec.Command("grep", "hello") |
Here we explicitly grab input/output pipes, start the process, write some input to it, read the resulting output, and finally wait for the process to exit. |
grepIn, _ := grepCmd.StdinPipe() grepOut, _ := grepCmd.StdoutPipe() grepCmd.Start() grepIn.Write([]byte("hello grep\ngoodbye grep")) grepIn.Close() grepBytes, _ := io.ReadAll(grepOut) grepCmd.Wait() |
We omitted error checks in the above example, but
you could use the usual |
fmt.Println("> grep hello") fmt.Println(string(grepBytes)) |
Note that when spawning commands we need to
provide an explicitly delineated command and
argument array, vs. being able to just pass in one
command-line string. If you want to spawn a full
command with a string, you can use |
lsCmd := exec.Command("bash", "-c", "ls -a -l -h") lsOut, err := lsCmd.Output() if err != nil { panic(err) } fmt.Println("> ls -a -l -h") fmt.Println(string(lsOut)) } |
The spawned programs return output that is the same as if we had run them directly from the command-line. |
$ go run spawning-processes.go > date Wed Oct 10 09:53:11 PDT 2012 |
> grep hello hello grep |
|
> ls -a -l -h drwxr-xr-x 4 mark 136B Oct 3 16:29 . drwxr-xr-x 91 mark 3.0K Oct 3 12:50 .. -rw-r--r-- 1 mark 1.3K Oct 3 16:28 spawning-processes.go |
Next example: Exec'ing Processes.