Created
January 26, 2021 14:29
-
-
Save wperron/0b40fd9dcf57ff6b092c264dbfe73dee to your computer and use it in GitHub Desktop.
Add a namespace prefix to the output of a subprocess
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() { | |
pr, pw := io.Pipe() | |
go do(pw) | |
scanner := bufio.NewScanner(pr) | |
for scanner.Scan() { | |
fmt.Printf("subprocess-1 | %s\n", scanner.Text()) | |
} | |
} | |
func do(w io.Writer) { | |
cmd := exec.Command("ping", "google.com") | |
cmd.Stdout = w | |
if err := cmd.Start(); err != nil { | |
log.Fatal(err) | |
} | |
if err := cmd.Wait(); err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment