A summary of the IO method instance of Go language _golang

Source: Internet
Author: User
Tags readfile

Type Pipewriter

Copy Code code as follows:

Type Pipewriter struct {
Contains filtered or unexported fields
}

(1) Func (w *pipewriter) Close () error closes the pipe, the read operation in progress will return EOF if there is still unread data in the pipe, and the subsequent reading is still normal
Copy Code code as follows:

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 This function and read inside the Closewitherror is very similar, close the pipe, The read operation that is in progress at shutdown returns the exception passed in the parameter, and can still be read normally if there is still unread data in the pipeline
Copy Code code as follows:

Import (
"Errors"
"FMT"
"IO"
)

Func Main () {
R, W: = Io. Pipe ()
Go W.write ([]byte ("Hello Widuu"))
Newerr: = errors. New ("Your Daye suddenly closed")
W.closewitherror (Newerr)
Data: = Make ([]byte, 10)
_, Err: = R.read (data)
If Err!= nil {
Fmt. PRINTLN (Err)//your Daye suddenly shut down.
}
}


(3) Func (w *pipewriter) write (data []byte) (n int, err error) finally came to write, this is to write a byte slice into the pipeline, the return is written bytes and error, the front used too much, any one bar
Copy Code code as follows:

Import (
"FMT"
"IO"
)

Func Main () {
R, W: = Io. Pipe ()
Go W.write ([]byte ("Hello Widuu"))//write is []byte, note that the official document writes that the write pipeline is blocked until the end of all data reads
Data: = Make ([]byte, 11)
N, _: = R.read (data)
Fmt. Println (String (data))//hello Widuu
Fmt. PRINTLN ("read number", N)//read Number 10
}


Type Reader

Copy Code code as follows:

Type Reader Interface {
Read (P []byte) (n int, err error)
}

(1) Func Limitreader (R reader, n Int64) reader, we have previously said reader this structure, in fact, this is a reader of the package, limited it read bytes, in fact, he achieved is IO. limitedreader{} This structure
Copy Code code as follows:

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 this function to see is to encapsulate a number of readers, and the method is similar to the above, but only a number of packages, of course, but also to read the restrictions, we code for everyone to test
Copy Code code as follows:

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) Since the above introduction read, I introduce a write func multiwriter (writers ... Writer) Writer The same effect is only this time to write
Copy Code code as follows:

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, huh?
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 This method is meant to read data from R and then write to W, which has no internal buffer, look at the code
Copy Code code as follows:

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{}

Copy Code code as follows:

Type Sectionreader struct {
Contains filtered or unexported fields
}


(1) Func Newsectionreader (R Readerat, off int64, n Int64) *sectionreader, you can see that, in fact, this is the way to get IO. Sectionreader, the first parameter reader, the second parameter offset, and the third parameter is how much to read

Copy Code code as follows:

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) The familiar read () is actually reading the data used, we see the function can be understood, because we often encounter this on the two wrote this ~ ~
Copy Code code as follows:

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) Amount This is the same as the previous readat, except that there is only one offset, less interception number, But you need to know what Sectionreader is doing to intercept the data, so there's no need to intercept the numbers.
Copy Code code as follows:

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) This is a cheap amount to set the file pointer, and we have a seek in the OS, Offsets the read start point, current read point, and end point of the sectionreader, offset offset, whence set option 0: Read start point, 1: Current read point, 2: End point (Bad), others: will throw Seek:invalid whence exception
Copy Code code as follows:

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 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 readat ()
}


(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, let's look at the code
Copy Code code as follows:

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 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
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.