dth981485742 2009-12-29 21:07
浏览 129
已采纳

在Go中模拟TCP连接

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?

  • 写回答

3条回答 默认 最新

  • dpymrcl269187540 2009-12-30 17:39
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?