-
-
Save adam12/e98de452dd7663dce9cd3db86f8df61f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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