Buffered read

bufio package provides buffered read functions. Let’s see an example:

(1) Create a test.txt file first:

  1. # cat test.txt
  2. abcd
  3. efg
  4. hijk
  5. lmn

You can see test.txt contains 4 lines.

(2) See the following program:

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "log"
  7. "os"
  8. )
  9. func main() {
  10. f, err := os.Open("test.txt")
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. r := bufio.NewReader(f)
  15. for {
  16. if s, err := r.ReadSlice('\n'); err == nil || err == io.EOF {
  17. fmt.Printf("%s", s)
  18. if err == io.EOF {
  19. break
  20. }
  21. } else {
  22. log.Fatal(err)
  23. }
  24. }
  25. }

(a)

  1. f, err := os.Open("test.txt")

Open test.txt file.

(b)

  1. r := bufio.NewReader(f)

bufio.NewReader(f) creates a bufio.Reader struct which implements buffered read function.

(c)

  1. for {
  2. if s, err := r.ReadSlice('\n'); err == nil || err == io.EOF {
  3. fmt.Printf("%s", s)
  4. if err == io.EOF {
  5. break
  6. }
  7. } else {
  8. log.Fatal(err)
  9. }
  10. }

Read and print each line.

The running result is here:

  1. abcd
  2. efg
  3. hijk
  4. lmn

We can also use bufio.Scanner to implement above “print each line” function:

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. func main() {
  9. f, err := os.Open("test.txt")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. s := bufio.NewScanner(f)
  14. for s.Scan() {
  15. fmt.Println(s.Text())
  16. }
  17. }

(a)

  1. s := bufio.NewScanner(f)

bufio.NewScanner(f) creates a new bufio.Scanner struct which splits the content by line by default.

(b)

  1. for s.Scan() {
  2. fmt.Println(s.Text())
  3. }

s.Scan() advances the bufio.Scanner to the next token (in this case, it is one optional carriage return followed by one mandatory newline), and we can use s.Text() function to get the content.

We can also customize SplitFunc function which doesn’t separate content by line. Check the following code:

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. func main() {
  9. f, err := os.Open("test.txt")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. s := bufio.NewScanner(f)
  14. split := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
  15. for i := 0; i < len(data); i++ {
  16. if data[i] == 'h' {
  17. return i + 1, data[:i], nil
  18. }
  19. }
  20. return 0, data, bufio.ErrFinalToken
  21. }
  22. s.Split(split)
  23. for s.Scan() {
  24. fmt.Println(s.Text())
  25. }
  26. }

The split function separates the content by “h“, and the running result is:

  1. abcd
  2. efg
  3. ijk
  4. lmn