type PipeWriter
復(fù)制代碼 代碼如下:
type PipeWriter struct {
// contains filtered or unexported fields
}
(1)func (w *PipeWriter) Close() error關(guān)閉管道,關(guān)閉時正在進(jìn)行的Read操作將返回EOF,若管道內(nèi)仍有未讀取的數(shù)據(jù),后續(xù)仍可正常讀取
復(fù)制代碼 代碼如下:
import (
"fmt"
"io"
)
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
}
(2)func (w *PipeWriter) CloseWithError(err error) error這個函數(shù)和read里邊的CloseWithError是大同小異的,關(guān)閉管道,關(guān)閉時正在進(jìn)行的Read操作將返回參數(shù)傳入的異常,若管道內(nèi)仍有未讀取的數(shù)據(jù),后續(xù)仍可正常讀取
復(fù)制代碼 代碼如下:
import (
"errors"
"fmt"
"io"
)
func main() {
r, w := io.Pipe()
go w.Write([]byte("hello widuu"))
newerr := errors.New("your daye 突然關(guān)閉了")
w.CloseWithError(newerr)
data := make([]byte, 10)
_, err := r.Read(data)
if err != nil {
fmt.Println(err) //your daye 突然關(guān)閉了
}
}
(3)func (w *PipeWriter) Write(data []byte) (n int, err error)終于來打write了,這個是把字節(jié)切片寫入管道,返回的是寫入字節(jié)數(shù)和error,前邊用到的太多了,隨便哪一個吧
復(fù)制代碼 代碼如下:
import (
"fmt"
"io"
)
func main() {
r, w := io.Pipe()
go w.Write([]byte("hello widuu")) //寫入的是[]byte,注意官方文檔寫的是,寫入管道阻塞,一直到所有數(shù)據(jù)的讀取結(jié)束
data := make([]byte, 11)
n, _ := r.Read(data)
fmt.Println(string(data)) //hello widuu
fmt.Println("read number", n) //read number 10
}
type Reader
復(fù)制代碼 代碼如下:
type Reader interface {
Read(p []byte) (n int, err error)
}
(1)func LimitReader(r Reader, n int64) Reader,我們之前就說了Reader這個結(jié)構(gòu),其實這就是對Reader的一次封裝,限定了它讀取字節(jié)數(shù),其實他實現(xiàn)的就是io.LimitedReader{}這個結(jié)構(gòu)
復(fù)制代碼 代碼如下:
import (
"fmt"
"io"
"os"
"reflect"
)
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
}
}
(2)func MultiReader(readers ...Reader) Reader這個函數(shù)一看就知道是封裝了多個readers,跟上邊的方法差不多,只是封裝了多個而已,當(dāng)然還去除了讀取的限制,我們代碼給大家測試一下
復(fù)制代碼 代碼如下:
import (
"fmt"
"io"
"os"
"reflect"
)
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
}
(3)既然上邊介紹讀了,我這介紹個寫吧type Write`func MultiWriter(writers ...Writer) Writer一樣的作用只不過是這次換成寫了
復(fù)制代碼 代碼如下:
import (
"fmt"
"io"
"io/ioutil"
"os"
)
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
}
(4)func TeeReader(r Reader, w Writer) Reader這個方法有意思是從r中讀取數(shù)據(jù)然后寫入到w中,這個沒有內(nèi)部緩沖區(qū),看下代碼
復(fù)制代碼 代碼如下:
import (
"fmt"
"io"
"os"
"reflect"
)
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{}
復(fù)制代碼 代碼如下:
type SectionReader struct {
// contains filtered or unexported fields
}
(1)func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader,你一看就知道了,其實就是通過這個方法獲取到io.SectionReader,第一個參數(shù)讀取器,第二個參數(shù)偏移量,第三個參數(shù)是讀取多少
復(fù)制代碼 代碼如下:
import (
"fmt"
"io"
"os"
"reflect"
)
func main() {
f, _ := os.Open("test.txt")
sr := io.NewSectionReader(f, 2, 5)
fmt.Println(reflect.TypeOf(sr)) //*io.SectionReader
}
(2)func (s *SectionReader) Read(p []byte) (n int, err error)熟悉的read()其實就是讀取數(shù)據(jù)用的,大家看函數(shù)就可以理解了,因為咱們經(jīng)常遇到這個上兩個都寫這個了~~
復(fù)制代碼 代碼如下:
import (
"fmt"
"io"
"os"
)
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
}
(3)func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err error)額這個跟之前的ReadAt是一樣的,只不過只有一個偏移量,少了截取數(shù),但是你要知道SectionReader做的是什么就把數(shù)據(jù)截取了,所以就不需要截取數(shù)了
復(fù)制代碼 代碼如下:
import (
"fmt"
"io"
"os"
)
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
}
}
(4)func (s *SectionReader) Seek(offset int64, whence int) (int64, error)這個是設(shè)置文件指針的便宜量的,之前我們的os里邊也是有個seek的,對SectionReader的讀取起始點、當(dāng)前讀取點、結(jié)束點進(jìn)行偏移,offset 偏移量,whence 設(shè)定選項 0:讀取起始點,1:當(dāng)前讀取點,2:結(jié)束點(不好用),其他:將拋出Seek: invalid whence異常
復(fù)制代碼 代碼如下:
import (
"fmt"
"io"
"os"
)
func main() {
f, _ := os.Open("test.txt")
defer f.Close()
sr := io.NewSectionReader(f, 2, 5)
p := make([]byte, 10)
sr.Seek(1, 0) //相當(dāng)于起始的地址偏移1
n, err := sr.Read(p)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(p[:n])) //lo w 是不是達(dá)到了前邊的ReadAt()
}
(5)func (s *SectionReader) Size() int64返回的是可以讀取的字節(jié)數(shù),這個不受偏移指針的影響,也不受當(dāng)前讀取的影響,我們具體看下代碼
復(fù)制代碼 代碼如下:
import (
"fmt"
"io"
"os"
)
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) //相當(dāng)于起始的地址偏移1
n, err := sr.Read(p)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(p[:n])) //lo w
fmt.Println(sr.Size()) //5
}
您可能感興趣的文章:- 深入解析Go語言的io.ioutil標(biāo)準(zhǔn)庫使用
- Go語言中io.Reader和io.Writer的詳解與實現(xiàn)
- Go語言的IO庫那么多糾結(jié)該如何選擇