title: How to clear a bytes.Buffer that is set as StdOut in exec.Command in Golang (b.Reset is not working)
body: <p>I'm trying to write a command line application wrapper in GoLang that exposes a REST API to interact with said application and show its output. </p>
  
  <p>Basically when the REST API server starts, it should start a command line application that keeps running (As an example just <em>Powershell.exe</em>) and should be able to interact with the application and show its output when requested trough the API. </p>
  
  <p>So far the server starts, the child process also starts, and I'm able to issue commands to the process and retrieve its output. To capture the output of the process I'm using a <em>bytes.Buffer</em> that I attach to the processes <em>Stdout</em> and <em>Stderr</em>.</p>
  
  <p>The problem is that that output buffer seems to be growing into infinite, and there is no way to clear it. Running "<em>.Reset()</em>" on that buffer seems to be completely ignored, and the full buffered output is always shown.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>package main
  
  import (
      "bytes"
      "fmt"
      "log"
      "net/http"
      "os/exec"
  )
  
  func main() {
  
      var c = exec.Command("powershell.exe")
      var b bytes.Buffer
      c.Stdout = &amp;b
      c.Stderr = &amp;b
  
      stdin, err := c.StdinPipe()
      if err != nil {
          log.Fatal(err)
      }
  
      http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
          fmt.Fprintf(w, "Do stuff")
      })
  
      http.HandleFunc("/clear", func(w http.ResponseWriter, r *http.Request) {
          fmt.Fprintf(w, "Cleared")
          b.Reset()
      })
  
      http.HandleFunc("/write", func(w http.ResponseWriter, r *http.Request) {
          stdin.Write([]byte("echo hi\n"))
          fmt.Fprintf(w, "OK")
      })
  
      http.HandleFunc("/show", func(w http.ResponseWriter, r *http.Request) {
          fmt.Fprintf(w, string(b.Bytes()))
      })
  
      fmt.Println("SERVER 1 STARTING")
      c.Start()
      c.CombinedOutput()
      http.ListenAndServe(":8081", nil)
      fmt.Println("SERVER 1 STOPPED")
  
  }
  </code></pre>
  
  <p>So the expected behavior here is that when you call "<em>/write</em>" it will execute "echo hi" in the child application. "<em>/show</em>" will show the output buffer of the application. and "<em>/clear</em>" should clear the output buffer, so that when you run "<em>/write</em>", then "<em>/clear</em>" and then "<em>/show</em>", an empty string should be shown. But in this case, it keeps showing the application output that never gets cleared. </p>
  
  <p>Interestingly if I manually write into the buffer via <em>b.WriteString()</em> and then I do the <em>b.Reset()</em>, that content does gets erased, but not the content from the command line application. </p>
  
  <p>Is there some way to properly clear the bytes.Buffer when it's attached to Stdout/Stderr? Or alternatively, is there some way to limit the buffer to a specific number of lines?</p>