Go Package bufio: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 6: Line 6:


=Overview=
=Overview=
The <code>bufio</code> package is aimed at buffering input in something faster (memory) while interacting with something slower (disk).
=Reading a Text File Line by Line=
=Reading a Text File Line by Line=
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>

Revision as of 18:40, 7 February 2024

External

Internal

Overview

The bufio package is aimed at buffering input in something faster (memory) while interacting with something slower (disk).

Reading a Text File Line by Line

import (
	"bufio"
	"os"
)

f, err := os.Open("/Users/ovidiu/tmp/sometext.txt")
if err != nil {
  ...
}
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
  line := scanner.Text()
  ...
}