bufio在golang中的修改

huangapple go评论72阅读模式
英文:

Modyfication of bufio in golang

问题

我正在阅读一个大文件,并通过HTTP POST发送该文件。
我使用bufio。

现在我想修改这个文件的第一行,怎么做?

f := bufio.NewReaderSize(os.Stdin, 65536)
bufPart, err := f.Peek(65536)
//如何修改bufPart(f)?
...
req, err := http.NewRequest("POST", url, f)
英文:

I reading big file and sending this file by http POST.
I use bufio.

And now I want to modify one of first line of this file, how to do it ?

f := bufio.NewReaderSize(os.Stdin, 65536)
bufPart, err := f.Peek(65536))
//how to modify bufPart(f) ?
...
req, err := http.NewRequest("POST", url, f)

答案1

得分: 2

两种方法可以实现:

  1. 创建自己的 Reader 实现,包装一个 bufio.Reader,并实现替换逻辑(需要计算读取的字节数)。

  2. 调用 io.Pipe,将返回的 PipeReader 传递给 NewRequest,并启动一个单独的 goroutine 从文件中读取数据,修改后写入返回的 PipeWriter。

希望这样说得清楚。

英文:

Two ideas how to do it:

  1. Create your own Reader implementation that wraps an bufio.Reader and implements replacing logic (you will have to count number of read bytes).

  2. Call io.Pipe, pass the returned PipeReader to NewRequest and start a separate goroutine that will read data from a file, modify it and write to the returned PipeWriter.

Hope this makes sense.

huangapple
  • 本文由 发表于 2015年8月19日 15:57:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/32089693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定