io.SectionReader和io.LimitReader

package main

import (
   "fmt"
   "os"
   "io/ioutil"
   "io"
)

func main() {
   file, err := os.OpenFile("test.txt", os.O_RDWR, 777) //make一个reader
   if err != nil {
      panic(err)
   }
   r := io.NewSectionReader(file, 4, 10) //规定范围
   buf, err := ioutil.ReadAll(r)
   fmt.Println(string(buf))

   lr := io.LimitReader(file, 12) //规定最大能读取到哪里
   buf, err = ioutil.ReadAll(lr)
   fmt.Println(string(buf))
}

最终结果输出

 is io-tes
this is io-t

猜你喜欢

转载自my.oschina.net/u/1766862/blog/1784942
Io
IO: