This is a creation in Article, where the information may have evolved or changed.
Today we continue to talk about the Golang standard library IO Library, we put the IO library is finished today, so we say no more, give us the explanation and code
[1]type Reader
[PHP]
Type Reader Interface {
Read (P []byte) (n int, err error)
}
[/php]
(1) func LimitReader(r Reader, n int64) Reader
, we said before that the structure of reader, in fact, this is the reader of a package, limit the number of bytes read it, in fact, he realized is IO. limitedreader{} This structure
[PHP]
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
}
}
[/php]
(2) func MultiReader(readers ...Reader) Reader
This function at a glance know is encapsulated a plurality of readers, with the method of the above, just encapsulated a number of it, of course, but also to remove the limit of reading, we code to test you
[PHP]
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
}
[/php]
(3) Since the introduction of the above reading, I introduce this to write the type Write`func MultiWriter(writers ...Writer) Writer
same function is just this one to write
[PHP]
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"))
Don't be so logical, I'm testing it, ha.
R1, _: = Ioutil. ReadFile ("1.txt")
R2, _: = Ioutil. ReadFile ("2.txt")
Fmt. Println (String (r1))//widuu
Fmt. Println (String (r2))//widuu
}
[/php]
(4) func TeeReader(r Reader, w Writer) Reader
This method is meant to read the data from R and then write to W, which has no internal buffer, look at the code
[PHP]
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
}
[/php]
[2]type SectionReader{}
[PHP]
Type Sectionreader struct {
Contains filtered or unexported fields
}
[/php]
(1) func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader
, you know at a glance, is actually through this method to get to IO. Sectionreader, the first parameter reader, the second parameter offset, and the third parameter is how much to read
[PHP]
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
}
[/php]
(2) func (s *SectionReader) Read(p []byte) (n int, err error)
The familiar read () is actually reading the data with, we see the function can be understood, because we often meet this two have written this ~ ~
[PHP]
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
}
[/php]
(3) func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err error)
This is the same as the previous readat, except that there is only one offset and less interception, but you need to know what sectionreader do to intercept the data, so you don't have to intercept the numbers.
[PHP]
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
}
}
[/php]
(4) func (s *SectionReader) Seek(offset int64, whence int) (int64, error)
This is a cheap set of file pointers, before our OS also has a seek, the Sectionreader read start point, the current reading point, the end point offset, offset offset, whence set option 0: Read starting point, 1: The current reading point, 2: the end point (not used), others: will throw Seek:invalid whence exception
[PHP]
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)//equivalent to the starting address offset 1
N, Err: = Sr. Read (P)
If err! = Nil {
Fmt. PRINTLN (ERR)
}
Fmt. Println (String (P[:n]))//lo W is not reaching the front of the ReadAt ()
}
[/php]
(5) func (s *SectionReader) Size() int64
Returns the number of bytes that can be read, which is unaffected by the offset pointer, and is not affected by the current read, and we look at the code specifically
[PHP]
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)//equivalent to the starting address offset 1
N, Err: = Sr. Read (P)
If err! = Nil {
Fmt. PRINTLN (ERR)
}
Fmt. Println (String (p[:n))//lo W
Fmt. Println (Sr. Size ())//5
}
[/php]
Today we will finish the standard library of Io, and which to speak tomorrow? Let's focus on tomorrow, huh! Every day only a little Golang standard library, convenient for everyone to learn and use, more time to understand the standard library, we do more hands, if you like please continue to follow us!
Code and files hosted on GitHub, GitHub address https://github.com/widuu/gopkg
Golang Standard Library
Without permission, may not reprint this station any article: the Micro Degree Network»golang Explanation (go language) standard library analysis The IO end article