title: Reading from stdout pipe once ready in golang
body: <p>I'm facing with a weird golang issue. The following code will clarify:</p>
  
  <pre><code>package main
  
  import (
      "os/exec"
      "io"
      "fmt"
      "os"
  )
  
  var (
      pw io.WriteCloser
      pr io.ReadCloser
  )
  
  func main() {
      term := exec.Command("/bin/sh")
  
      // Get stdin writer pipe
      pw, _ = term.StdinPipe()
      pr, _ = term.StdoutPipe()
  
      term.Start()
  
      run("cd ~")
      pwd := run("pwd");
  
      // Do something with pwd output
      ...
  
      term.Wait()
  }
  
  func run(c string) string {
      io.WriteString(pw, fmt.Sprintln(c))
  
      buf := make([]byte, 32 * 1024)
      pr.Read(buf)
      return string(buf)
  }
  </code></pre>
  
  <p>I'd like to run some commands in a shell env and read their output. There's no problem on write/run command but it seems that there're some limitations while reading: </p>
  
  <ul>
  <li>you can't know if a command doesn't output anything or not;</li>
  <li>there's no way to check if stdout is ready to be read or not.</li>
  </ul>
  
  <p>The <code>pr.Read(dest)</code> method will block the code flow until something is read from stdout. As said, the goal is to read sequentially (without using a go routine and/or an infinite loop). This means that if we send a <code>cd</code> command the func end is never reached.</p>
  
  <p>Setting the non-block flag through <code>unix.SetNonblock</code> on stdout file descriptor seems to solve the above issue but you can't know prior if it's ready or not and an error saying "resource temporary not available" is returned from <code>.Read</code> call.</p>