时间:2020-11-28 14:44:50 | 栏目:Golang | 点击:次
type PipeWriter
func main() {
r, w := io.Pipe()
go w.Write([]byte("hello word"))
data := make([]byte, 10)
n, err := r.Read(data)
w.Close()
if err == io.EOF {
fmt.Println("executing read return EOF")
fmt.Println("executing read reads number", n)
}
n, _ = r.Read(data)
fmt.Println(string(data)) //hello word
fmt.Println("next read number", n) //next read number 0
}
func main() {
r, w := io.Pipe()
go w.Write([]byte("hello widuu"))
newerr := errors.New("your daye 突然关闭了")
w.CloseWithError(newerr)
data := make([]byte, 10)
_, err := r.Read(data)
if err != nil {
fmt.Println(err) //your daye 突然关闭了
}
}
func main() {
r, w := io.Pipe()
go w.Write([]byte("hello widuu")) //写入的是[]byte,注意官方文档写的是,写入管道阻塞,一直到所有数据的读取结束
data := make([]byte, 11)
n, _ := r.Read(data)
fmt.Println(string(data)) //hello widuu
fmt.Println("read number", n) //read number 10
}
type Reader
func main() {
f, _ := os.Open("test.txt")
defer f.Close()
reader := io.LimitReader(f, 5)
p := make([]byte, 5)
fmt.Println(reflect.TypeOf(reader)) //*io.LimitedReader
var total int
for {
n, err := reader.Read(p)
if err == io.EOF {
fmt.Println("read value", string(p[:total])) //read value hello
fmt.Println(total) //5
break
}
total = total + n
}
}
func main() {
f1, _ := os.Open("test1.txt")
f2, _ := os.Open("test.txt")
defer f1.Close()
defer f2.Close()
reader := io.MultiReader(f1, f2) //*io.multiReader
fmt.Println(reflect.TypeOf(reader))
p := make([]byte, 10)
var total int
var data string
for {
n, err := reader.Read(p)
if err == io.EOF {
fmt.Println("read end", total) //read end 17
break
}
total = total + n
data = data + string(p[:n])
}
fmt.Println("read value", data) //read value widuu2hello widuu
fmt.Println("read count", total) // read count 17
}
func main() {
f1, _ := os.Create("1.txt")
f2, _ := os.Create("2.txt")
writer := io.MultiWriter(f1, f2)
writer.Write([]byte("widuu"))
//千万别这么逻辑来 ,我这是测试用的哈
r1, _ := ioutil.ReadFile("1.txt")
r2, _ := ioutil.ReadFile("2.txt")
fmt.Println(string(r1)) //widuu
fmt.Println(string(r2)) //widuu
}
func main() {
r, _ := os.Open("test.txt")
w, _ := os.Create("test2.txt")
reader := io.TeeReader(r, w)
fmt.Println(reflect.TypeOf(reader)) //*io.teeReader
p := make([]byte, 10)
n, _ := reader.Read(p)
fmt.Println(string(p[:n])) //hello widu
}
type SectionReader{}
(1)func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader,你一看就知道了,其实就是通过这个方法获取到io.SectionReader,第一个参数读取器,第二个参数偏移量,第三个参数是读取多少
func main() {
f, _ := os.Open("test.txt")
sr := io.NewSectionReader(f, 2, 5)
fmt.Println(reflect.TypeOf(sr)) //*io.SectionReader
}
func main() {
f, _ := os.Open("test.txt")
defer f.Close()
sr := io.NewSectionReader(f, 2, 5)
p := make([]byte, 10)
n, err := sr.Read(p)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(p[:n])) //llo w
}
func main() {
f, _ := os.Open("test.txt")
defer f.Close()
sr := io.NewSectionReader(f, 2, 5)
p := make([]byte, 10)
n, err := sr.ReadAt(p, 1)
if err == io.EOF {
fmt.Println(string(p[:n])) // lo w
}
}
func main() {
f, _ := os.Open("test.txt")
defer f.Close()
sr := io.NewSectionReader(f, 2, 5)
p := make([]byte, 10)
sr.Seek(1, 0) //相当于起始的地址偏移1
n, err := sr.Read(p)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(p[:n])) //lo w 是不是达到了前边的ReadAt()
}
func main() {
f, _ := os.Open("test.txt")
defer f.Close()
sr := io.NewSectionReader(f, 2, 5)
fmt.Println(sr.Size()) //5
p := make([]byte, 10)
sr.Seek(1, 0) //相当于起始的地址偏移1
n, err := sr.Read(p)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(p[:n])) //lo w
fmt.Println(sr.Size()) //5
}