This is a creation in Article, where the information may have evolved or changed.
Golang Standard Library
Today we are explaining the Io/ioutil package inside the Golang standard library –
package io/ioutil
1.
ioutil.ReadDir(dirname string)
The prototype of this function is like this
func ReadDir(dirname string) ([]os.FileInfo, error)
It is not difficult to see the input is the dirname type is a string type such as "D:/go", it will be a fileinfo slice, where the structure of FileInfo is like this
[PHP]
Type FileInfo Interface {
Name () string//filename
Size () Int64//sang file sizes
Mode () FileMode//file permissions
Modtime () time. Time//Times
Isdir () bool//is the directory
Sys () interface{}//Basic data source interface (can return nil)
}
[/php]
So the returned slice can execute the FileInfo method, and what is the other parameter? Error is returned successfully! This time we can do the experiment code is such a demo
[PHP]
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 (), "Creation Time", V.modtime ())
Fmt. Println (V.name (), "System Information", V.sys ())
If v.isdir () = = true {
Fmt. Println (V.name (), "is Directory")
}
}
}
[/php]
2. The explanation is that the prototype of the ioutil.ReadFile(filename string)
function isfunc ReadFile(filename string) ([]byte, error)
The input is a string type, which returns a byte-type slice and a err this is very simple we sit down code demo
[PHP]
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))
}
[/php]
3. The third one we're explaining is that the prototype of the ioutil.ReadAll()
function is func ReadAll(r io.Reader) ([]byte, error)
an IO input. The reader meta-reader returns the []byte byte slice and error
[PHP]
Import (
"FMT"
"Io/ioutil"
"Reflect"
"Strings"
)
Func Main () {
Reader: = strings. Newreader ("Hello word widuu")//Return to *strings. Reader
Fmt. Println (reflect. TypeOf (reader))
Data, _: = Ioutil. ReadAll (reader)
Fmt. Println (String (data))
}
[/php]
4. The fourth one is ioutil. The Nopcloser () function prototype is the Func nopcloser (r io. Reader) io. Readcloser is also a reader and then returns the Readcloser interface, which provides the Close method, the top method is perfect after the demo
[PHP]
Import (
"FMT"
"Io/ioutil"
"Reflect"
"Strings"
)
Func Main () {
Reader: = strings. Newreader ("Hello word widuu")//Return to *strings. Reader
r: = Ioutil. Nopcloser (reader)
Defer R.close ()
Fmt. Println (reflect. TypeOf (reader))
Data, _: = Ioutil. ReadAll (reader)
Fmt. Println (String (data))
}
[/php]
5. The fifth is a common temporary directory ioutil.TempDir()
function prototype is the func TempDir(dir, prefix string) (name string, err error)
input directory name, prefix, return name is prefix+ random number
[PHP]
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 in the front
}
[/php]
6. The last one, since you can create a directory, you can create ioutil.TempFile()
a file function prototype is the func TempFile(dir, prefix string) (f *os.File, err error)
input directory name, prefix, return the file pointer and error
[PHP]
Import (
"FMT"
"Io/ioutil"
)
Func Main () {
File, Error: = Ioutil. Tempfile ("D:/test", "tmp")
Defer file. Close ()
If error! = Nil {
Fmt. Println ("Failed to create file")
Return
}
File. WriteString ("Hello word")//WriteString () Details of the file pointer are shown in the OS. WriteString ()
Filedata, _: = Ioutil. ReadFile (file. Name ())
Fmt. Println (String (filedata))
}
[/php]
This article started in the Micro Network-Network Technology Center release URL Http://www.widuu.com then posted in my Go language blog http://www.lingphp.com if you reprint please indicate the source
Without permission, may not reprint this station any article: the Micro Degree Network»golang Explanation (go language) standard library analysis Io.ioutil