In-depth parsing of the Go Language Io.ioutil standard library using _golang

Source: Internet
Author: User

Today we're going to talk about the Io/ioutil package inside the Golang standard library--that's package io/ioutil
1.ioutil. ReadDir (DirName String) The prototype of this function is such a func ReadDir (dirname string) ([]os. FileInfo, error)
It's not hard to see that the DirName type is a string type such as "D:/go", but it's a fileinfo slice, where the fileinfo structure is this

Copy Code code as follows:

Type FileInfo Interface {
Name () string//file names
Size () Int64//sing the file sizes
Mode () FileMode//file permissions
Modtime () time. Time//Times
Isdir () bool//is a directory
Sys () interface{}//Underlying data source interface (can return nil)
}

So the returned slice can perform the FileInfo method, and what is the other parameter? Error whether successful return! This time we can do the experimental code is such a demo
Copy Code code as follows:

Import "FMT"
Import "Io/ioutil"

Func Main () {
Dir_list, E: = Ioutil. ReadDir ("D:/test")
If e!= Nil {
Fmt. Println ("read dir error")
Return
}
For I, V: = Range Dir_list {
Fmt. Println (i, "=", V.name ())
Fmt. Println (V.name (), "Permission is:", V.mode ())
Fmt. Println (V.name (), "File Size:", v.size ())
Fmt. Println (V.name (), "Create Time", V.modtime ())
Fmt. Println (V.name (), "System Information", V.sys ())
If v.isdir () = = true {
Fmt. Println (V.name (), "is Directory")

}
}
}


2. The explanation is ioutil. The prototype of the ReadFile (filename string) function is func ReadFile (filename string) ([]byte, error)
The string type is entered, the byte slice is returned and a err is a simple one. Let's look at the code demo
Copy Code code as follows:

Import (
"FMT"
"Io/ioutil"
"OS"
)

Func Main () {
Data, err: = Ioutil. ReadFile ("D:/test/widua.go")
If Err!= nil {
Fmt. PRINTLN ("read error")
Os. Exit (1)
}
Fmt. Println (String (data))
}


3. The third one we are explaining is ioutil. The prototype of the ReadAll () function is func readall (r io). Reader) ([]byte, error) is entered as an IO. The reader meta Reader returns a []byte byte slice and error
Copy Code code as follows:

Import (
"FMT"
"Io/ioutil"
"Reflect"
"Strings"
)

Func Main () {
Reader: = strings. Newreader ("Hello word widuu")//back to *strings. Reader
Fmt. Println (reflect. TypeOf (reader))
Data, _: = Ioutil. ReadAll (reader)
Fmt. Println (String (data))
}


4. The fourth one is ioutil. The Nopcloser () function prototype is the Func nopcloser (R io). Reader) io. Readcloser is still a reader and then returns the Readcloser interface, which provides the Close method, the top method is perfect after the demo
Copy Code code as follows:

Import (
"FMT"
"Io/ioutil"
"Reflect"
"Strings"
)

Func Main () {
Reader: = strings. Newreader ("Hello word widuu")//back to *strings. Reader
r: = Ioutil. Nopcloser (reader)
Defer R.close ()
Fmt. Println (reflect. TypeOf (reader))
Data, _: = Ioutil. ReadAll (reader)
Fmt. Println (String (data))
}


5. The fifth is the common temporary directory ioutil. The TempDir () function prototype is func tempdir (dir, prefix string) (name string, err Error) input directory name, prefix, and return name is prefix+ random number
Copy Code code as follows:

Import (
"FMT"
"Io/ioutil"
)

Func Main () {
Dir, err: = Ioutil. TempDir ("D:/test", "tmp")
If Err!= nil {
Fmt. PRINTLN ("Common temp directory Failed")
Return
}
Fmt. Println (dir)//Return D:\test\tmp846626247 is the prefix+ random number at the front
}


6. The last one can create a file ioutil If you can create a directory. The Tempfile () function prototype is func tempfile (dir, prefix string) (f *os. File, err Error) Enter directory name, prefix, return pointer and error
Copy Code code as follows:

Import (
"FMT"
"Io/ioutil"
)

Func Main () {
File, Error: = Ioutil. Tempfile ("D:/test", "tmp")
Defer file. Close ()
If error!= nil {
Fmt. Println ("Create file Failed")
Return
}
File. WriteString ("Hello word")//WriteString () Details of the file pointer see OS. WriteString ()
Filedata, _: = Ioutil. ReadFile (file. Name ())
Fmt. Println (String (filedata))
}


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.