Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
75 lines (61 sloc) 2.64 KB
Title: 62.Spawning_Processes.go
Author: OpenSource
Date: 2017-05-23
Description: For Study
Sometimes our Go programs need to spawn other, non-Go processes.
For example, the syntax highlighting on this site is implemented
by spawning a pygmentize process from a Go program.
Let’s look at a few examples of spawning processes from Go.
package main
import "os/exec"
import "io/ioutil"
import "fmt"
func main(){
// We’ll start with a simple command that takes no arguments or input and just prints something to stdout. The exec.Command helper creates an object to represent this external process.
dateCmd := exec.Command("date")
// .Output is another helper that handles the common case of running a command,
// waiting for it to finish, and collecting its output.
// If there were no errors, dateOut will hold bytes with the date info.
dateOut, err := dateCmd.Output()
if err != nil{
fmt.Println("> date")
grepCmd := exec.Command("grep", "hello")
// Next we’ll look at a slightly more involved case
// where we pipe data to the external process on its stdin
// and collect the results from its stdout.
// 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()
grepIn.Write([]byte("hello grep\ngoodbye grep"))
grepBytes, _ := ioutil.ReadAll(grepOut)
// We ommited error checks in the above example, but you could use the usual
// if err != nil pattern for all of them.
// We also only collect the StdoutPipe results, but you could collect the StderrPipe
// in exactly the same way.
fmt.Println("> grep hello")
// 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 bash’s -c option:
lsCmd := exec.Command("bash", "-c", "ls -a -l -h")
lsOut, err := lsCmd.Output()
if err != nil {
fmt.Println("> ls -a -l -h")
// The spawned programs return output that is the same as if we had run them directly
// from the command-line.
You can’t perform that action at this time.