doutan5844 2018-09-06 13:45
浏览 63
已采纳

GoLang链接io.Reader

I'm trying to implement a proxy pattern to chain transformations on io.Reader, in order to handle chunk of bytes efficiently.

  1. We cannot use pointers on receivers, so my solution seem not very efficient

  2. The code below say "process take too long"

Complete example at: https://play.golang.org/p/KhM0VXLq4CO

b := bytes.NewBufferString(text)
t := transformReaderHandler(*b)
readByChunk(t)

type transformReaderHandler bytes.Buffer

func (t transformReaderHandler) Read(p []byte) (n int, err error) {
    n, err = (*bytes.Buffer)(&t).Read(p)
    //if n > 0 {
    //  Do Something on the chunk
    //}
    return
}

Do you have any more efficient (memory efficient, computationally efficient) solution ?

Why do this code is not working ?

EDIT: The implementation of @svsd solution : https://play.golang.org/p/VUpJcyKLB6D

package main

import (
    "io"
    "fmt"
    "bytes"
)

const text = "Reaaaaally long and complex text to read in chunk"

func main() {
    b := bytes.NewBufferString(text)

    t := (*transformReaderHandler)(b)

    readByChunk(t)
}

type transformReaderHandler bytes.Buffer

func (t *transformReaderHandler) Read(p []byte) (n int, err error) {
    n, err = (*bytes.Buffer)(t).Read(p)
    if n > 0 {
        p[0] = 'X'
    }
    return
}

func readByChunk(r io.Reader) {
    var p = make([]byte, 4)

    for {
        n, err := r.Read(p)
        if err == io.EOF {
            break
        }
        fmt.Println(string(p[:n]))
    }
}
  • 写回答

2条回答 默认 最新

  • dongqin1819 2018-09-06 14:06
    关注

    The code below say "process take too long"

    Why do this code is not working ?

    In the transformReaderHandler.Read() method, you have a value receiver. That means each time Read() is called, it gets a copy of the instance on which it was called. Then when you then call (*bytes.Buffer)(&t).Read(p), it modifies the internal state of that instance so that next time when you read, it reads from after the point it read earlier.

    Now because the instance is a copy, it is discarded after the method exits and the original instance remains unchanged. Hence, each time you call Read(), bytes.Buffer.Read() reads only the first few bytes. To prove this, add a statement fmt.Println("n=", n, "err=", err) inside readByChunk() after calling Read().

    To quickly check that this is indeed due to the value receiver, you can define transformReaderHandler.Read() with a pointer receiver and store t as t = (*transformReaderHandler)(b). I'll let you examine what it does. (edit: the correct solution involving embedding is in the comments)

    Do you have any more efficient (memory efficient, computationally efficient) solution ?

    If you're only looking for buffered IO for more efficient reads, look at the bufio.NewReader(). If that's not sufficient, you can take inspiration from it and wrap around an io.Reader interface instead of wrapping over a bytes.Buffer instance.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd