SoFunction
Updated on 2025-03-04

Commonly used file reading methods in GO language

This article describes the commonly used file reading methods in GO language. Share it for your reference. The specific analysis is as follows:

Golang has many methods for reading files. I didn’t know how to choose when I first started, so I posted it here and checked it quickly.

One-time reading

Small files are recommended to read at one time, so that the program is simpler and the fastest.

Copy the codeThe code is as follows:
func ReadAll(filePth string) ([]byte, error) {
 f, err := (filePth)
 if err != nil {
  return nil, err
 }

 return (f)
}


There are even simpler methods, I often use (filePth)

Block reading

A good balance between speed and memory footprint.

Copy the codeThe code is as follows:
package main

import (
 "bufio"
 "io"
 "os"
)

func processBlock(line []byte) {
 (line)
}

func ReadBlock(filePth string, bufSize int, hookfn func([]byte)) error {
 f, err := (filePth)
 if err != nil {
  return err
 }
 defer ()

buf := make([]byte, bufSize) //How many bytes are read at a time
 bfRd := (f)
 for {
  n, err := (buf)
hookfn(buf[:n]) // n is the number of bytes successfully read

if err != nil { //Return immediately after encountering any errors and ignore the EOF error message
   if err == {
    return nil
   }
   return err
  }
 }

 return nil
}

func main() {
 ReadBlock("", 10000, processBlock)
}

Read line by line

Line-by-line reading is sometimes really convenient, the performance may be slower, but it only takes up very little memory space.

Copy the codeThe code is as follows:
package main

import (
 "bufio"
 "io"
 "os"
)

func processLine(line []byte) {
 (line)
}

func ReadLine(filePth string, hookfn func([]byte)) error {
 f, err := (filePth)
 if err != nil {
  return err
 }
 defer ()

 bfRd := (f)
 for {
  line, err := ('\n')
hookfn(line) //Put it before error processing. Even if an error occurs, the data that has been read will be processed.
if err != nil { //Return immediately after encountering any errors and ignore the EOF error message
   if err == {
    return nil
   }
   return err
  }
 }
 return nil
}

func main() {
 ReadLine("", processLine)
}

I hope this article will be helpful to everyone's GO language programming.