OS / exec优雅,循环兼容的stdin和stdout输入/输出

Example script is just wrapper to "wc -m" command, simple symbol counter. I trying just feed input with "teststrings" slice elements. And receive number of symbol of each string at output listener goroutine. Looking for a way to make "wc" listen forever for input at all. I'v notice when i increase sleep to

time.Sleep(6000 * time.Nanosecond)

wc don't wait for input.

package main

import (
    "bytes"
    "fmt"
    "os/exec"
    "time"
)

func main() {
    BashCommand := exec.Command("wc", "-m")
    InputBytes := &bytes.Buffer{}
    OutputBytes := &bytes.Buffer{}
    BashCommand.Stdin = InputBytes
    BashCommand.Stdout = OutputBytes
    e := BashCommand.Start()
    time.Sleep(1 * time.Nanosecond)
    _, _ = InputBytes.Write([]byte("13symbolsting"))
    if e != nil {
        fmt.Println(e)
    }
    fmt.Println("after run")

    teststrings := []string{
        "one",
        "twoo",
        "threeeee",
    }
    for _, s := range teststrings {
        _, _ = InputBytes.Write([]byte(s))

    }

    //result printer
    go func() {
        for {
            line, _ := OutputBytes.ReadString('
')
            if line != "" {
                fmt.Println(line)
            }
        }
    }()
    var input string
    fmt.Scanln(&input) //dont exit until keypress

}

If you increase the sleep to a large value, the goroutine started by the command to pump InputBytes to the process runs before data is written to InputBytes. The goroutine closes the pipe to the child and exits without having read any data.

Use pipes instead of bytes.Buffer:

c := exec.Command("wc", "-m")
w, _ := c.StdinPipe()
r, _ := c.StdoutPipe()
if err := c.Start(); err != nil {
    log.Fatal(err)
}

w.Write([]byte("13symbolsting"))
teststrings := []string{
    "one",
    "twoo",
    "threeeee",
}
for _, s := range teststrings {
    w.Write([]byte(s))

}
w.Close() // Close pipe to indicate input is done.

var wg sync.WaitGroup
wg.Add(1)

go func() {
    s := bufio.NewScanner(r)
    for s.Scan() {
        fmt.Println(s.Text())
    }
    wg.Done()
}()

wg.Wait()

Another option is to write to the bytes.Buffer before starting the command and wait for command to complete before reading the output:

c := exec.Command("wc", "-m")
var w, r bytes.Buffer
c.Stdin = &w
c.Stdout = &r

// Write data before starting command.

w.Write([]byte("13symbolsting"))
teststrings := []string{
    "one",
    "twoo",
    "threeeee",
}
for _, s := range teststrings {
    w.Write([]byte(s))

}

if err := c.Start(); err != nil {
    log.Fatal(err)
}

// Wait for command to complete before reading data.

if err := c.Wait(); err != nil {
    log.Fatal(err)
}

s := bufio.NewScanner(&r)
for s.Scan() {
    fmt.Println(s.Text())
}