golang LimitReader TeeReader Buffer例子
发布时间:2020-12-16 19:17:56 所属栏目:大数据 来源:网络整理
导读:用到buffer的地方要加 还有就是如果知道想要读的数据的长度 TeeReader 好像并没有 LimitReader 好用,因为要创建一个 Writer ,就相当于要新开一块内存 这个地方的go的库函数代码都是可以比较容易看懂的 package main import ( "strings" "io" "bytes" "fmt"
用到buffer的地方要加 package main
import (
"strings"
"io"
"bytes"
"fmt"
"io/ioutil"
)
func test1(){
x:="123456789"
r:=strings.NewReader(x)
b:=bytes.Buffer{}
r_new:=io.TeeReader(io.LimitReader(r,5),&b)
out1,_:=ioutil.ReadAll(r_new)
out2,_:=ioutil.ReadAll(r)
out3,_:=ioutil.ReadAll(&b)
fmt.Println("out1",string(out1))
fmt.Println("out2:",string(out2))
fmt.Println("buffer",string(out3))
fmt.Println()
}
func test2() {
x:="123456789"
r:=strings.NewReader(x)
r_new:=io.LimitReader(r,5)
out1,_:=ioutil.ReadAll(r)
fmt.Println("out1",string(out2))
fmt.Println()
}
func main(){
test1()
test2()
}
/* output: out1 12345 out2: 6789 buffer 12345 out1 12345 out2: 6789 */ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |