In depth analysis of the Go language's io.ioutil standard library usage

  • 2020-05-30 20:22:31
  • OfStack

Today we're going to talk about the io/ioutil package next to the golang standard library, which is package io/ioutil
The prototype of this function is func ReadDir(dirname string) ([] os.FileInfo, error).
It is not difficult to see that the input type of dirname is of type string, such as "d:/go". However, it will be a slice of FileInfo, in which the structure of FileInfo is like this


  type FileInfo interface {
    Name() string       // Name of file
    Size() int64        // Sung file size
    Mode() FileMode     // File permissions
    ModTime() time.Time // time
    IsDir() bool        // Is it a directory
    Sys() interface{}   // Underlying data source interface (can return nil)
}

So the returned slice can execute the FileInfo method. What's the other parameter? Whether error returned successfully! At this point we can do one experiment and the code looks like this: 1 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())
  fmt.Println(v.Name(), " The permissions are :", v.Mode())
  fmt.Println(v.Name(), " The 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 ")   }
 }
}

2. The prototype of ioutil.ReadFile (filename string) function is func ReadFile(filename string) ([]byte, error).
I'm going to type string, and I'm going to return a byte slice and I'm going to return an err and that's a very simple thing to do let's 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 is that the prototype of the ioutil.ReadAll () function is func ReadAll(r io.Reader) ([]byte, error). The input is an io.Reader meta-reader and the return is []byte byte slice and error

import (
 "fmt"
 "io/ioutil"
 "reflect"
 "strings"
) func main() {
 reader := strings.NewReader("hello word widuu") // return *strings.Reader
 fmt.Println(reflect.TypeOf(reader))
 data, _ := ioutil.ReadAll(reader)
 fmt.Println(string(data))
}

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

import (
 "fmt"
 "io/ioutil"
 "reflect"
 "strings"
) func main() {
 reader := strings.NewReader("hello word widuu") // return *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) enter the directory name, prefix, return name is prefix+ random number

import (
 "fmt"
 "io/ioutil"
) func main() {
 dir, err := ioutil.TempDir("D:/test", "tmp")
 if err != nil {
  fmt.Println(" Common temporary directory failures ")
  return
 }
 fmt.Println(dir)  // Returns the D:\test\tmp846626247 That's the one in front prefix+ The random number
}

6. The last one can create ioutil.TempFile () function prototype is func TempFile(dir, prefix string) (f * os.File, err error). Enter the directory name, prefix, and return the pointer to the file and error

import (
 "fmt"
 "io/ioutil"
) func main() {
 file, error := ioutil.TempFile("D:/test", "tmp")
 defer file.Close()
 if error != nil {
  fmt.Println(" File creation failed ")
  return
 }
 file.WriteString("Hello word") // using file Pointer to the WriteString() see os.WriteString()
 filedata, _ := ioutil.ReadFile(file.Name())
 fmt.Println(string(filedata))
}


Related articles: