Golang (go language) standard library analysis Io (1)

Source: Internet
Author: User
Tags readfile
This is a creation in Article, where the information may have evolved or changed.

Today we open a new standard library IO is also to continue the last time we did not finish the standard library, last time we only said Io/ioutil this time we continue to talk about the entire IO library

(1) func Copy(dst Writer, src Reader) (written int64, err error) This function reads a copy from one file to another and copies it to the EOF of the read file, so it does not return IO. EOF error, parameter is write to target and read target, return int64 copy byte number and err information
[PHP]
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)//return Int64 11 open my write.txt is test.txt inside the Hello Widuu
}
[/php]

(2) func CopyN(dst Writer, src Reader, n int64) (written int64, err error) Look at the function to know the same as above, just add a limit to read the number, and then we look at the code

[PHP]
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
}
[/php]

(3) func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error) This function is to read the data from the reader into our buf, limit the minimum number of bytes read, if we read the data is less than the minimum reader, such as you set min value is 8, but you read the number of bytes of data is 5 will return an ' IO. Errunexpectedeof ', if greater than will return ' IO. Errshortbuffer ', read will have ' IO. EOF ' ~ ~, speak more ha, this reader as long as we meet this interface can use this
[PHP]
Type Reader Interface {
Read (P []byte) (n int, err error)
}
[/php]
We talk about the OS package when we found, which *file support func (f *File) Read(b []byte) (n int, err error) , do not understand you can look back

[PHP]
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 Data 8
Fmt. Println ("Read fewer Value:", String (B[0:n]))
}

If err = = Io. errshortbuffer{//This is our set buf that 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
}
}
[/php]

(4) func ReadFull(r Reader, buf []byte) (n int, err error) This function is similar to the function above, except that it reads Len (BUF) and puts it in BUF
[PHP]
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
}
[/php]

(5) func WriteString(w Writer, s string) (n int, err error) Finished reading, of course, with the write, this function is written to the target write the word A, the return is the number of bytes written and error errors, mainly the error of permissions, which write Ah! It's all writer, and this structure can be written

[PHP]
Type Writer Interface {
Write (P []byte) (n int, err error)
}
[/php]

Like read, our *file is there func (f *File) Write(b []byte) (n int, err error) , of course, in fact, we have wirtestring in our *file.func (f *File) WriteString(s string) (ret int, err error)

[PHP]
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 () The wood has the right to 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
}
[/php]

(6) Today the last one we talk about a struct type Limitedreader

[PHP]
Type Limitedreader struct {
R Reader//Reader
N Int64//maximum byte limit
}
[/php]

[1] Only one method is realized func (l *LimitedReader) Read(p []byte) (n int, err error) we are not hard to find this is the rhythm of our brother, Readatlast.

[PHP]
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

}

}
[/php]

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 (go Language) standard library analysis Io (1)

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.