Skip to content

Instantly share code, notes, and snippets.

@croese
Last active December 23, 2019 16:40
Show Gist options
  • Save croese/7bbec75f29a97a4e0064be26e1f1061b to your computer and use it in GitHub Desktop.
Save croese/7bbec75f29a97a4e0064be26e1f1061b to your computer and use it in GitHub Desktop.
Test go code with new processes and pipes
package main
import (
"log"
"os"
"os/exec"
)
func main() {
read, write, _ := os.Pipe()
ls := exec.Command("ls", "-l")
ls.Stdout = write
ls.Stderr = os.Stderr
ls.Dir = "/Users/croese"
grep := exec.Command("grep", `\sP`)
grep.Stdin = read
r, w, err := os.Pipe()
if err != nil {
log.Fatalln(err)
}
grep.Stdout = w
grep.Stderr = os.Stderr
wc := exec.Command("wc", "-l")
wc.Stdin = r
wc.Stdout = os.Stdout
wc.Stderr = os.Stderr
err = ls.Start()
if err != nil {
log.Fatalln(err)
}
err = grep.Start()
if err != nil {
log.Fatalln(err)
}
err = wc.Start()
if err != nil {
log.Fatalln(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment