tags:

views:

77

answers:

1

In Go, a TCP connection (net.Conn) is a io.ReadWriteCloser. I'd like to test my network code by simulating a TCP connection. There are two requirements that I have:

  1. the data to be read is stored in a string
  2. whenever data is written, I'd like it to be stored in some kind of buffer which I can access later

Is there a data structure for this, or an easy way to make one?

+2  A: 

Why not using bytes.Buffer? It's io.Reader/io.Writer and has String() method to get the stored data. If you need to make it io.ReadWriteCloser, you could define you own type:

type CloseableBuffer struct { bytes.Buffer }

and define Close method:

func (b *CloseableBuffer) Close() os.Error { return nil }

Ivan Krasin