title: How to test a function's output (stdout/stderr) in unit tests
body: <p>I have a simple function I want to test:</p>
  
  <pre><code>func (t *Thing) print(min_verbosity int, message string) {
      if t.verbosity &gt;= minv {
          fmt.Print(message)
      }
  }
  </code></pre>
  
  <p>But how can I test what the function actually sends to standard output? <a href="http://search.cpan.org/~bdfoy/Test-Output-1.03/lib/Test/Output.pm" rel="noreferrer">Test::Output</a> does what I want in Perl.  I know I could write all my own boilerplate to do the same in Go (as described <a href="https://stackoverflow.com/a/10476304/13860">here</a>):</p>
  
  <pre><code>orig = os.Stdout
  r,w,_ = os.Pipe()
  thing.print("Some message")
  var buf bytes.Buffer
  io.Copy(&amp;buf, r)
  w.Close()
  os.Stdout = orig
  if(buf.String() != "Some message") {
      t.Error("Failure!")
  }
  </code></pre>
  
  <p>But that's a lot of extra work for every single test.  I'm hoping there's a more standard way, or perhaps an abstraction library to handle this.</p>