title: How to pipe stdin in golang?
body: <p>I'm connecting a pipe to my <code>stdout</code> in golang as follows for the purpose of testing (I stripped out the error handling codes):</p>
  
  <pre><code>stdout := os.Stdout
  defer func() {
    os.Stdout = stdout
  }()
  
  r, w, _ = os.Pipe()
  os.Stdout = w
  
  var buf bytes.Buffer
  fmt.Print(testOut)
  w.Close()
  io.Copy(&amp;buf, r)
  assert.Equal(t, testOut, buf.String())
  </code></pre>
  
  <p>I want to basically do something similar with <code>stdin</code> as well. Something like this:</p>
  
  <pre><code>stdin := os.Stdin
  defer func() {
    os.Stdin = stdin
  }()
  
  r, w, _ = os.Pipe()
  os.Stdin = r
  
  var in string
  w.WriteString(testIn)
  // w.Close()
  fmt.Scan(&amp;in)
  assert.Equal(t, testIn, in)
  </code></pre>
  
  <p>But, I cannot get this working. If I don't close <code>w</code>, <code>fmt.Scan(&amp;in)</code> never returns. If I close <code>w</code>, I get <code>file already closed</code> error.</p>
  
  <p>Any I idea how to get this working?</p>