title: Go: receive os.cmd stdout and stderr in order
body: <p>I need to execute subcommand from go and process it stdout and stderr separately, with keeping order of ouput that comes to stdin/stdout. I've tried several differents ways, but could not achieve the correct order of output; following code shows that ouput handling order is absolutely random:</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "log"
      "os/exec"
  )
  
  var (
      result = ""
  )
  
  type writer struct {
      result string
      write  func(bytes []byte)
  }
  
  func (writer *writer) Write(bytes []byte) (int, error) {
      writer.result += string(bytes) // process result later
      result += string(bytes)
      return len(bytes), nil
  }
  
  func main() {
      cmd := exec.Command("bash", "-c", "echo TEST1; echo TEST2 1&gt;&amp;2; echo TEST3")
  
      stderr := &amp;writer{}
      cmd.Stderr = stderr
  
      stdout := &amp;writer{}
      cmd.Stdout = stdout
  
      err := cmd.Start()
      if err != nil {
          log.Fatal(err)
      }
  
      err = cmd.Wait()
      if err != nil {
          log.Fatal(err)
      }
  
      fmt.Println(result)
  }
  </code></pre>
  
  <p>With several runs code can output following:</p>
  
  <pre><code>$ go run main.go
  TEST1
  TEST3
  TEST2
  </code></pre>
  
  <p>I expect following result in all cases:</p>
  
  <pre><code>$ go run main.go
  TEST1
  TEST2
  TEST3
  </code></pre>
  
  <p>I can not call cmd.CombinedOutput because I need to process stdout/stderr separately and in realtime.</p>