Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use io.ioutil Standard Library in Go language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the Go language how to use the io.ioutil standard library related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Go language how to use the io.ioutil standard library article will have a harvest, let's take a look.

The prototype of the function 1.ioutil.ReadDir (dirname string) is func ReadDir (dirname string) ([] os.FileInfo, error).

It is not difficult to see that the input type is dirname type is string type, such as "d:/go", but it will be a slice of FileInfo, where the structure of FileInfo is like this

Type FileInfo interface {Name () string / / the name of the file Size () int64 / / sang the file size Mode () FileMode / / permissions ModTime () time.Time / / time IsDir () bool / / whether it is a directory Sys () interface {} / / basic data source interface (can return nil)}

So the returned slice can execute the FileInfo method, what is the other parameter? Whether error returned successfully! At this time, we can do an experiment. The code is a demo.

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

The permissions of fmt.Println (v.Name (), "are:", 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 a directory")

}

}

}

two。 The prototype of the ioutil.ReadFile (filename string) function is func ReadFile (filename string) ([] byte, error).

You enter the string type, and you return a byte slice and an err. This is very simple. Let's take a look at the code demo.

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 thing we're going to talk about is that the prototype of the ioutil.ReadAll () function is func ReadAll (r io.Reader) ([] byte, error), which inputs an io.Reader meta-reader and returns [] byte byte slices and error.

The copy code is as follows:

Import (

"fmt"

"io/ioutil"

"reflect"

"strings"

)

Func main () {

Reader: = strings.NewReader ("hello word widuu") / / returns * strings.Reader

Fmt.Println (reflect.TypeOf (reader))

Data, _: = ioutil.ReadAll (reader)

Fmt.Println (string (data))

}

4. The fourth is whether the prototype of the ioutil.NopCloser () function is func NopCloser (r io.Reader) io.ReadCloser or a Reader, and then returns the ReadCloser interface, providing the Close method, and the demo after the above method is improved.

The copy code is as follows:

Import (

"fmt"

"io/ioutil"

"reflect"

"strings"

)

Func main () {

Reader: = strings.NewReader ("hello word widuu") / / returns * 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.TempDir () function prototype is func TempDir (dir, prefix string) (name string, err error) input directory name, prefix, the returned name is a prefix+ random number

The copy code is as follows:

Import (

"fmt"

"io/ioutil"

)

Func main () {

Dir, err: = ioutil.TempDir ("D:/test", "tmp")

If err! = nil {

Fmt.Println ("Common temporary directory failure")

Return

}

Fmt.Println (dir) / / returns D:\ test\ tmp846626247, which is the prefix+ random number preceding it.

}

6. Finally, since you can create a directory, you can create a file ioutil.TempFile () function prototype is func TempFile (dir, prefix string) (f * os.File, err error) enter the directory name, prefix, and return the file pointer and error

The copy code is 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") / / uses the WriteString () of the file pointer

Filedata, _: = ioutil.ReadFile (file.Name ())

Fmt.Println (string (filedata))

}

This is the end of the article on "how to use the io.ioutil Standard Library in the Go language". Thank you for reading! I believe that everyone has a certain understanding of "how to use the io.ioutil standard library in Go language". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report