1.func copy (DST Writer, SRC Reader) (written int64, err error) This function is copied from one file to another file and is copied to the EOF that reads the file, so it does not return IO. EOF error, parameter is write to target and read target, return int64 copy bytes and err information
Copy Code code as follows:
Import (
"FMT"
"IO"
"OS"
)
Func Main () {
R, _: = OS. Open ("Test.txt")
W, _: = OS. Create ("Write.txt")
num, err: = Io. Copy (W, W)
If Err!= nil {
Fmt. PRINTLN (ERR)
}
Fmt. PRINTLN (num)//back to Int64 11 open my write.txt is test.txt inside of Hello Widuu
}
2.func Copyn (DST Writer, Src Reader, n Int64) (written int64, err error) See the function is the same as the above, just add a limit to the number of reads, and then we look at the code
Copy Code code as follows:
Import (
"FMT"
"IO"
"Io/ioutil"
"OS"
)
Func Main () {
R, _: = OS. Open ("Test.txt")
W, _: = OS. Create ("Write1.txt")
num, err: = Io. Copyn (W, R, 5)
If Err!= nil {
Fmt. PRINTLN (ERR)
}
Defer R.close ()
B, _: = Ioutil. ReadFile ("Write1.txt")
Fmt. Println (string (b))//Output Hello
Fmt. PRINTLN (num)//5
}
3.func Readatleast (R Reader, buf []byte, Min int) (n int, err error) This function is to read data from the reader into our buf, limit the minimum number of bytes read, If the data we read is less than the minimum reader, for example, you set min's value to be 8, but the number of bytes you read is 5, which returns an IO. Errunexpectedeof ', if greater than will return ' IO. Errshortbuffer ', reading will have ' IO. EOF ' ~, say some more ha, this reader as long as we meet this interface can use this
Copy Code code as follows:
Type Reader Interface {
Read (P []byte) (n int, err error)
}
where *file supports func (f *file) Read (b []byte) (n int, err error)
Copy Code code as follows:
Import (
"FMT"
"IO"
"OS"
)
Func Main () {
R, _: = OS. Open ("Write1.txt")
B: = make ([]byte, 20)
Defer R.close ()
var total int
for {
N, err: = Io. Readatleast (R, B, 8)
If Err = = Nil {
Fmt. Println ("Read enough value:", string (b))//Read enough Value:hello Widuu
}
If err = = Io. errunexpectedeof {//Read data less than our limited minimum read 8
Fmt. Println ("Read fewer Value:", String (b[0:n))
}
If err = = Io. errshortbuffer{//This is the BUF that we set, which is b less than we limit 8.
Fmt. Println ("buf Too Short")
Os. Exit (1)
}
If err = = Io. EOF {//Finished reading output
Fmt. Println (' Read end Total ', total]//read End Total 11
Break
}
Total = Total + N
}
}
4.func Readfull (R Reader, buf []byte) (n int, err error) This function is similar to the function above, except to read Len (BUF) and place it in the BUF
Copy Code code as follows:
Import (
"FMT"
"IO"
"OS"
)
Func Main () {
R, _: = OS. Open ("Write.txt")
B: = make ([]byte, 20)
num, err: = Io. Readfull (R, B)
Defer R.close ()
If err = = Io. EOF {
Fmt. Println ("Read End Total", num)
}
If err = = Io. errunexpectedeof {
Fmt. Println ("Read fewer Value:", String (B[:num]))//read fewer Value:hello Widuu, still buf length greater than read length
Return
}
Fmt. Println ("Read Value:", string (b))//if B is 5, it appears here.
}
5.func WriteString (w Writer, S string) (n int, err error) finished reading, of course, with to write, this function is written to write the target of the word Fu Yi, return is the number of bytes written and error errors, mainly the permissions of the error, Which writes! It's all writer this structure can be written
Copy Code code as follows:
Type Writer Interface {
Write (P []byte) (n int, err error)
}
Like read, our *file is func (f *file) Write (b []byte) (n int, err error), of course *file is already in our wirtestring func (f *file) Writestrin G (S string) (ret int, err error)
Import (
"FMT"
"IO"
"Io/ioutil"
"OS"
)
Func Main () {
W, _: = OS. OpenFile ("Write1.txt", OS. O_RDWR, OS. Modeperm)
N, err: = Io. WriteString (w, "Ni hao Ma")
If Err!= nil {
Fmt. PRINTLN (ERR)//When I use Os.open () when the wood has permission sad ~ ~ Output Write Write1.txt:Access is denied.
}
Defer W.close ()
B, _: = Ioutil. ReadFile ("Write1.txt")
Fmt. Println ("Write Total", N)//write Total 9
Fmt. Println (string (b))//Ni hao Ma
}
6.type Limitedreader
Copy Code code as follows:
Type Limitedreader struct {
R Reader/Reader
N Int64//MAX byte limit
}
Only one method is implemented Func (L *limitedreader) Read (P []byte) (n int, err error) Actually, it's not hard for us to find that this is the rhythm of our brothers.
Copy Code code as follows:
Import (
"FMT"
"IO"
"OS"
)
Func Main () {
Reader, _: = OS. Open ("Test.txt")
Limitedreader: = io. limitedreader{
R:reader,
N:20,
}
P: = Make ([]byte, 10)
var total int
for {
N, Err: = Limitedreader. Read (P)
If err = = Io. EOF {
Fmt. Println ("Read Total", total)//read Total 11
Fmt. PRINTLN ("Read Value", String (p))//read value Hello Widuu
Break
}
Total = Total + N
}
}
7.type Pipereader
Copy Code code as follows:
Type Pipereader struct {
Contains filtered or unexported fields
}
(1) Func Pipe () (*pipereader, *pipewriter) creates a pipe and returns its reader and writer, which will be synchronized in memory, and its opening will be IO. Reader then waits for the input of io.writer, without internal buffering, it is safe to call read and write to each other and to invoke the parallel call written
Copy Code code as follows:
Import (
"FMT"
"IO"
"Reflect"
)
Func Main () {
R, W: = Io. Pipe ()
Fmt. Println (reflect. TypeOf (R))//*io. Pipereader
Fmt. Println (reflect. TypeOf (W))//*io. Pipewriter
}
(2) after the Func (R *pipereader) close () error pipe is closed, an ongoing or subsequent write writes operation returns ERRCLOSEDPIPE
Copy Code code as follows:
Import (
"FMT"
"IO"
)
Func Main () {
R, W: = Io. Pipe ()
R.close ()
_, Err: = W.write ([]byte ("Hello Widuu"))
If err = = Io. Errclosedpipe {
Fmt. PRINTLN ("Pipe is turned off cannot write")//pipe is turned off cannot be written
}
}
(3) Func (R *pipereader) Closewitherror (err Error) Error This is the top of R. When close closes, the writer returns the wrong information
Copy Code code as follows:
Import (
"Errors"
"FMT"
"IO"
)
Func Main () {
R, W: = Io. Pipe ()
R.close ()
ERR: = errors. New ("Pipe character turned off")//errors This package we've already said before, just one way new won't be able to look at the front
R.closewitherror (ERR)
_, Err = W.write ([]byte ("Test"))
If Err!= nil {
Fmt. The PRINTLN (ERR)//pipe character is closed
}
}
(4) Func (R *pipereader) read (data []byte) (n int, err error) Standard reading interface, which reads data from the pipe, blocks until a write interface is closed, and returns an error if there is an error in the write end, otherwise the EOF returned
Copy Code code as follows:
Import (
"FMT"
"IO"
)
Func Main () {
R, W: = Io. Pipe ()
Go W.write ([]byte ("Hello Widuu"))
D: = make ([]byte, 11)
N, _: = R.read (d)//read data from pipeline
Fmt. Println (String (d))
Fmt. PRINTLN (N)
}