SoFunction
Updated on 2025-03-04

Summary of common methods of golang reading files

Various ways to read files using Go language.

Loading into memory at one time

// * The entire file is read into memory, suitable for small files//Fixed bytes are read each time//The problem is prone to garbled code, because Chinese and Chinese symbols do not occupy one characterfunc readAllIntoMemory(filename string) (content []byte, err error) {
 fp, err := (filename) // Get file pointer if err != nil {
 return nil, err
 }
 defer ()
 fileInfo, err := ()
 if err != nil {
 return nil, err
 }
 buffer := make([]byte, ())
 _, err = (buffer) // Read the file content into the buffer if err != nil {
 return nil, err
 }
 return buffer, nil
}

Loading into memory at one time is suitable for small files. If the file is too large and the memory is tight, you can use buffering to read it in multiple times.

Block reading

// * Read one piece by one, that is, give a buffer, read it into the buffer in multiple times//Read by bytes and read the entire file to the buffer bufferfunc readByBlock(filename string) (content []byte, err error) {
 fp, err := (filename) // Get file pointer if err != nil {
 return nil, err
 }
 defer ()
 const bufferSize = 64 // Buffer size, read 64 bytes each time buffer := make([]byte, bufferSize)
 for {
 // Note that bytesRead here, otherwise there will be problems bytesRead, err := (buffer) // Read the file content into the buffer content = append(content, buffer[:bytesRead]...)
 if err != nil {
  if err ==  {
  err = nil
  break
  } else {
  return nil, err
  }
 }
 }
 return
}

Sometimes we need to process it by row

Read by line

// Read line by line, one line is a []byte, and multiple lines are [][] bytefunc readByLine(filename string) (lines [][]byte, err error) {
 fp, err := (filename) // Get file pointer if err != nil {
 return nil, err
 }
 defer ()
 bufReader := (fp)
 for {
 line, _, err := () // Read by line if err != nil {
  if err ==  {
  err = nil
  break
  }
 } else {
  lines = append(lines, line)
 }
 }
 return
}

Read all contents of a file using ioutil

func test1() {
 bytes,err := ("")
 if err != nil {
 (err)
 }
 ("total bytes read:",len(bytes))
 ("string read:",string(bytes))
}

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links