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
    }
}
3年前 评论
oursdreams (楼主) 3年前
讨论数量: 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
    }
}
3年前 评论
oursdreams (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!