Scanners
Read line by line.
Scanners is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is bufio.Scanner
bufio.Scanner is the easiest way to read input token by token, most commonly line by line.
It hides buffering and delimiter handling, and strips the trailing newline for you.
Scanning Lines
Create a scanner, then loop with Scan() which returns false at end. Read the current token with Text().
package main
import (
"bufio"
"fmt"
"strings"
)
func main() {
s := bufio.NewScanner(strings.NewReader("one\ntwo\nthree"))
for s.Scan() {
fmt.Println("line:", s.Text())
}
}Scanning Words
Set the split function with Split(bufio.ScanWords) to tokenize by whitespace instead of lines.
package main
import (
"bufio"
"fmt"
"strings"
)
func main() {
s := bufio.NewScanner(strings.NewReader("the quick brown fox"))
s.Split(bufio.ScanWords)
for s.Scan() {
fmt.Println(s.Text())
}
}Counting Tokens
Scanners make counting trivial. Here we count words.
package main
import (
"bufio"
"fmt"
"strings"
)
func main() {
s := bufio.NewScanner(strings.NewReader("a b c d e"))
s.Split(bufio.ScanWords)
count := 0
for s.Scan() {
count++
}
fmt.Println("words:", count)
}Bytes vs Text
Text() returns a string copy. Bytes() returns the underlying slice, which is reused on the next Scan() — copy it if you need to keep it.
package main
import (
"bufio"
"fmt"
"strings"
)
func main() {
s := bufio.NewScanner(strings.NewReader("data"))
s.Scan()
fmt.Println(len(s.Bytes()), "bytes")
}Scanning Runes
bufio.ScanRunes splits the input into UTF-8 runes, one token per character.
package main
import (
"bufio"
"fmt"
"strings"
)
func main() {
s := bufio.NewScanner(strings.NewReader("Gö"))
s.Split(bufio.ScanRunes)
for s.Scan() {
fmt.Printf("[%s]", s.Text())
}
fmt.Println()
}Checking for Errors
Scan() returning false can mean EOF or an error. After the loop, call s.Err() to distinguish them.
package main
import (
"bufio"
"fmt"
"strings"
)
func main() {
s := bufio.NewScanner(strings.NewReader("ok\nfine"))
for s.Scan() {
fmt.Println(s.Text())
}
if err := s.Err(); err != nil {
fmt.Println("error:", err)
}
}The Token Size Limit
By default a single token may not exceed bufio.MaxScanTokenSize (64KB). A longer line causes Scan() to stop with an error.
Growing the Buffer
Use Buffer(buf, max) to allow larger tokens. Pass an initial buffer and a maximum size.
package main
import (
"bufio"
"fmt"
"strings"
)
func main() {
s := bufio.NewScanner(strings.NewReader("long line here"))
buf := make([]byte, 0, 1024)
s.Buffer(buf, 1024*1024)
for s.Scan() {
fmt.Println(s.Text())
}
}Custom Split Functions
A split function has signature func(data []byte, atEOF bool) (advance int, token []byte, err error). You can split on commas, fixed widths, anything.
Scanner vs Reader
Scanner: simple, line/word tokens, auto-strips newlines, 64KB default limitReader: full control, no size limit, manual delimiter handling
Reach for Scanner first; drop to Reader when you need more.
Quick Check
Test your scanner knowledge.
Recap
You learned to read input token by token with bufio.Scanner.
Scan()+Text()/Bytes()loopSplitwith ScanLines/ScanWords/ScanRunes- Check
Err()after the loop - Use
Bufferfor tokens over 64KB
Frequently asked questions
Is the “Scanners” lesson free?
Yes — the full text of “Scanners” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Scanners”?
Read line by line. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Scanners” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.