Simulate a tcp connection in Go

后端 未结 4 1445
悲&欢浪女
悲&欢浪女 2020-12-29 15:46

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:

4条回答
  •  太阳男子
    2020-12-29 16:07

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

    type CloseableBuffer struct {
        bytes.Buffer
    }
    

    and define a Close method:

    func (b *CloseableBuffer) Close() error {
        return nil
    }
    

提交回复
热议问题