理解io/ioutil in go

  • package ioutil

    Package ioutil implements some I/O utility functions.

  • Variables

    • Discard is an io.Writer on which all Write calls succeed without doing anything.

      var Discard io.Writer = devNull(0)
      
  • Functions

    • ReadFile

      func ReadFile(filename string) ([]byte, error)

      ReadFile reads the file named by filename and returns the contents.

      A successful call returns err == nil, not err==EOF.

      Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported.

      package main
      import (
      	"fmt"
          "io/ioutil"
          "log"
      )
      
      func main(){
              
              
          content, err := ioutil.ReadFile("testdata/hello")
          if err != nil {
              
              
              log.Fatal(err)
          }
          
          fmt.Printf("File contents: %s", content)
      }
      

猜你喜欢

转载自blog.csdn.net/The_Time_Runner/article/details/109150761