go如何使用socket分段传输接收长文本

现有需求需将一个 50MB 大小的文本数据分段传输,谷歌到的 socket 分段传输都是对文件的分段传输。
文本如何做分段传输

最佳答案

这样行吗?

strReader := strings.NewReader( "In Go, input and output operations are achieved using primitives that model data as streams of bytes that can be read from or written to. To do this, the Go io package provides interfaces io.Reader and io.Writer, for data input and output operations respectively, as shown in the figure below")

reader := bufio.NewReader(strReader)
part = make([]byte, chunksize)

for {
    if count, err = reader.Read(part); err != nil {
        break
    }
    _, err = conn.Write(part[:count])
    if err != nil {
        return
    }
}
4年前 评论
oursdreams (楼主) 4年前
讨论数量: 1

这样行吗?

strReader := strings.NewReader( "In Go, input and output operations are achieved using primitives that model data as streams of bytes that can be read from or written to. To do this, the Go io package provides interfaces io.Reader and io.Writer, for data input and output operations respectively, as shown in the figure below")

reader := bufio.NewReader(strReader)
part = make([]byte, chunksize)

for {
    if count, err = reader.Read(part); err != nil {
        break
    }
    _, err = conn.Write(part[:count])
    if err != nil {
        return
    }
}
4年前 评论
oursdreams (楼主) 4年前