title: In-memory file for testing
body: <p>How does one create in-memory files for unit testing in Go?</p>
  
  <p>In Python, I test reading from a file or writing to a file using <a href="https://docs.python.org/3/library/io.html?highlight=stringio#io.BytesIO" rel="noreferrer"><code>io.BytesIO</code></a> or <a href="https://docs.python.org/3/library/io.html?highlight=stringio#io.StringIO" rel="noreferrer"><code>io.StringIO</code></a>. For example, to test a file parser, I would have</p>
  
  <pre><code>def test_parse_function():
      infile = io.StringIO('''\
  line1
  line2
  line3
  ''')
      parsed_contents = parse_function(infile)
      expected_contents = ['line1', 'line2', 'line3']  # or whatever is appropriate
      assert parsed_contents == expected_contents
  </code></pre>
  
  <p>Similarly for file output, I would have something like the following:</p>
  
  <pre><code>def test_write_function():
      outfile = io.StringIO()
      write_function(outfile, ['line1', 'line2', 'line3'])
      outfile.seek(0)
      output = outfile.read()
      expected_output = '''\
  line1
  line2
  line3
  '''
      assert output == expected_output
  </code></pre>