Skip to content

Instantly share code, notes, and snippets.

@icholy
Created February 20, 2018 18:54
Show Gist options
  • Save icholy/5df3c8d48f5c0d50b849aeb0bc17577b to your computer and use it in GitHub Desktop.
Save icholy/5df3c8d48f5c0d50b849aeb0bc17577b to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"io"
"log"
"os/exec"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
cmd := exec.Command("ls")
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
scanner := bufio.NewScanner(io.MultiReader(stdout, stderr))
for scanner.Scan() {
fmt.Println(scanner.Text())
}
if err := scanner.Err(); err != nil {
return err
}
return cmd.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment