Skip to content

Instantly share code, notes, and snippets.

@wperron
Created January 26, 2021 14:29
Show Gist options
  • Save wperron/0b40fd9dcf57ff6b092c264dbfe73dee to your computer and use it in GitHub Desktop.
Save wperron/0b40fd9dcf57ff6b092c264dbfe73dee to your computer and use it in GitHub Desktop.
Add a namespace prefix to the output of a subprocess
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