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:
- the data to be read is stored in a string
- 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?
Why not using
bytes.Buffer? It’s anio.ReadWriterand has aStringmethod to get the stored data. If you need to make it anio.ReadWriteCloser, you could define you own type:and define a
Closemethod: