The common way to read files in GO language

  • 2020-05-09 18:43:31
  • OfStack

This article illustrates the common file reading methods in the GO language. Share with you for your reference. The specific analysis is as follows:

Golang has many ways to read files, so I don't know how to choose when I just started, so I post it here for quick reference.

1 read

Small files are recommended to be read once, which makes the process simpler and faster.

func ReadAll(filePth string) ([]byte, error) {
 f, err := os.Open(filePth)
 if err != nil {
  return nil, err
 }  return ioutil.ReadAll(f)
}

There's an easier way, and I use ioutil.ReadFile (filePth) a lot.

reads

Good balance between speed and memory footprint.

package main
import (
 "bufio"
 "io"
 "os"
) func processBlock(line []byte) {
 os.Stdout.Write(line)
} func ReadBlock(filePth string, bufSize int, hookfn func([]byte)) error {
 f, err := os.Open(filePth)
 if err != nil {
  return err
 }
 defer f.Close()  buf := make([]byte, bufSize) //1 How many bytes are read at a time
 bfRd := bufio.NewReader(f)
 for {
  n, err := bfRd.Read(buf)
  hookfn(buf[:n]) // n Is the number of bytes successfully read   if err != nil { // Return immediately and ignore any errors encountered EOF The error message
   if err == io.EOF {
    return nil
   }
   return err
  }
 }  return nil
} func main() {
 ReadBlock("test.txt", 10000, processBlock)
}

Read line by line

Line-by-line reading is sometimes really convenient, and performance can be slow, but it takes up very little memory.

package main
import (
 "bufio"
 "io"
 "os"
) func processLine(line []byte) {
 os.Stdout.Write(line)
} func ReadLine(filePth string, hookfn func([]byte)) error {
 f, err := os.Open(filePth)
 if err != nil {
  return err
 }
 defer f.Close()  bfRd := bufio.NewReader(f)
 for {
  line, err := bfRd.ReadBytes('\n')
  hookfn(line) // In advance of error processing, even if an error occurs, the data that has been read is processed.
  if err != nil { // Return immediately and ignore any errors encountered EOF The error message
   if err == io.EOF {
    return nil
   }
   return err
  }
 }
 return nil
} func main() {
 ReadLine("test.txt", processLine)
}

I hope this article has been helpful to your programming of GO language.


Related articles: