Day 1: Syntax in Brief – wc
Goal: Use variables, slices, maps, loops and runes to write a
wcclone that counts exactly like the original – plus top words · Duration: approx. 90 min · Prerequisite: Day 0
Briefing
:=declares and assigns, the type is inferred; every variable has a zero value (0,"",false,nil).- A
stringis a sequence of bytes;rangeover a string yields runes (Unicode characters). “Grüße” has 7 bytes but 5 characters. - Slices (
[]string) grow withappend, maps (map[string]int) are hash tables; both are references to underlying data. - There is only one loop:
for. Nowhile, nodo. bufiobuffers input and output – essential when you read files character by character.
file/stdin ─bufio.Reader─► ReadRune() ─► count() ─► lines, words, bytes, chars
│
└─► map[string]int ─► topWords() ─► Top N
Drill 1: Syntax at a glance
mkdir -p ~/go-lab/tag01-wc && cd ~/go-lab/tag01-wc
go mod init example.com/wc
mkdir -p spielwiese cmd/wc
cat > spielwiese/main.go <<'EOF'
// spielwiese (playground): Go syntax at a glance.
package main
import (
"fmt"
"strings"
"unicode/utf8"
)
func main() {
// Variables: var with a type (zero value), := with type inference, const
var n int
gruss := "Grüße"
const limit = 3
fmt.Println("1)", n, gruss, limit)
// Strings are bytes, range yields runes (Unicode code points)
fmt.Println("2)", len(gruss), utf8.RuneCountInString(gruss))
for i, r := range gruss {
fmt.Printf("%d:%c ", i, r)
}
fmt.Println()
// Slices: append grows as needed, s[a:b] is a view
tools := []string{"kubectl", "helm"}
tools = append(tools, "bao")
fmt.Println("3)", tools, len(tools), tools[1:])
// Maps: access with comma-ok, a missing key yields the zero value
ports := map[string]int{"http": 80, "https": 443}
p, ok := ports["ssh"]
fmt.Println("4)", p, ok, len(ports))
// Loops: there is only for – classic, range, while-style
for i := range limit {
fmt.Print(i, " ")
}
words := strings.Fields(" go ist schnell ")
for len(words) > 0 {
fmt.Print(strings.ToUpper(words[0]), " ")
words = words[1:]
}
fmt.Println()
}
EOF
go run ./spielwieseExpected:
go: creating new go.mod: module example.com/wc
1) 0 Grüße 3
2) 7 5
0:G 1:r 2:ü 4:ß 6:e
3) [kubectl helm bao] 3 [helm bao]
4) 0 false 2
0 1 2 GO IST SCHNELL
Why? Line 2 shows the most important string fact in Go:
len counts bytes, range jumps from rune to
rune – the indexes 2 → 4 → 6 reveal that ü and
ß are two bytes each. for i := range limit
(since Go 1.22) counts from 0 to 2. A missing map key is not an error
but the zero value – ok tells you whether it was really
there.
Tip: Change the playground as much as you like. The compiler rejects unused variables and imports – read the message, it tells you exactly which line.
Save your progress? Sign up for free
Drill 2: Counting, rune by rune
The heart of wc: one function reads an input and counts
everything in a single pass.
cat > cmd/wc/count.go <<'EOF'
package main
import (
"bufio"
"io"
"strings"
"unicode"
)
// counts holds the counter values of one input (more on structs on Day 2).
type counts struct {
lines, words, bytes, chars int
}
// count reads r rune by rune. If freq is not nil, it also counts every word.
func count(r io.Reader, freq map[string]int) (counts, error) {
var c counts
var word strings.Builder
inWord := false
flush := func() { // finish the word and record it in the map
w := strings.TrimFunc(word.String(), unicode.IsPunct)
if freq != nil && w != "" {
freq[w]++
}
word.Reset()
}
br := bufio.NewReader(r)
for {
ch, size, err := br.ReadRune()
if err == io.EOF {
break
}
if err != nil {
return c, err
}
c.bytes += size
c.chars++
if ch == '\n' {
c.lines++
}
if unicode.IsSpace(ch) {
if inWord {
flush()
}
inWord = false
continue
}
if !inWord {
c.words++
inWord = true
}
word.WriteRune(unicode.ToLower(ch))
}
if inWord {
flush()
}
return c, nil
}
EOF
go vet ./cmd/wcExpected: no output – main is still missing, but
go vet already checks syntax and types.
Why like this? io.Reader is “anything you can read from”
– a file, stdin, the network. ReadRune returns characters
and their byte size, so bytes and characters are
correct at the same time. flush is a
closure: a function inside the function that shares
word and freq from the outside. A word starts
where something other than whitespace follows whitespace – exactly how
Unix wc counts too.
Save your progress? Sign up for free
Drill 3: Top words with a map and a slice
Maps have no order. To sort, you copy the entries into a slice:
cat > cmd/wc/top.go <<'EOF'
package main
import (
"cmp"
"slices"
)
// wordCount is a pair of word and frequency.
type wordCount struct {
word string
n int
}
// topWords returns the n most frequent words, alphabetically on a tie.
func topWords(freq map[string]int, n int) []wordCount {
list := make([]wordCount, 0, len(freq))
for w, k := range freq {
list = append(list, wordCount{w, k})
}
slices.SortFunc(list, func(a, b wordCount) int {
if c := cmp.Compare(b.n, a.n); c != 0 {
return c // descending by frequency
}
return cmp.Compare(a.word, b.word)
})
if len(list) > n {
list = list[:n]
}
return list
}
EOF
go vet ./cmd/wcExpected: no output.
Why? make([]wordCount, 0, len(freq)) reserves the right
capacity up front, so append never has to copy.
slices.SortFunc expects a comparison function that returns
<0, 0, >0 –
cmp.Compare returns exactly that. list[:n]
truncates without copying.
Save your progress? Sign up for free
Drill 4: main – flags, files, totals
cat > cmd/wc/main.go <<'EOF'
// wc counts lines, words, bytes and characters – like the Unix original.
// Day 1 from "Go hands-on".
package main
import (
"flag"
"fmt"
"os"
)
func main() {
lines := flag.Bool("l", false, "Zeilen zählen")
words := flag.Bool("w", false, "Wörter zählen")
bytes := flag.Bool("c", false, "Bytes zählen")
chars := flag.Bool("m", false, "Zeichen (Runen) zählen")
top := flag.Int("top", 0, "die `N` häufigsten Wörter ausgeben")
flag.Parse()
// Without a selection, like the original: lines, words, bytes.
if !*lines && !*words && !*bytes && !*chars {
*lines, *words, *bytes = true, true, true
}
show := func(c counts, name string) {
if *lines {
fmt.Printf("%8d", c.lines)
}
if *words {
fmt.Printf("%8d", c.words)
}
if *chars {
fmt.Printf("%8d", c.chars)
}
if *bytes {
fmt.Printf("%8d", c.bytes)
}
if name != "-" {
fmt.Print(" ", name)
}
fmt.Println()
}
var freq map[string]int // nil map: reading ok, writing forbidden
if *top > 0 {
freq = make(map[string]int)
}
files := flag.Args()
if len(files) == 0 {
files = []string{"-"} // "-" stands for stdin
}
var total counts
exitCode := 0
for _, name := range files {
c, err := countFile(name, freq)
if err != nil {
fmt.Fprintln(os.Stderr, "wc:", err)
exitCode = 1
continue
}
show(c, name)
total.lines += c.lines
total.words += c.words
total.bytes += c.bytes
total.chars += c.chars
}
if len(files) > 1 {
show(total, "total")
}
for i, wc := range topWords(freq, *top) {
fmt.Printf("%3d. %-20s %d\n", i+1, wc.word, wc.n)
}
os.Exit(exitCode)
}
// countFile opens name (or stdin for "-") and counts.
func countFile(name string, freq map[string]int) (counts, error) {
if name == "-" {
return count(os.Stdin, freq)
}
f, err := os.Open(name)
if err != nil {
return counts{}, err
}
defer f.Close() //nolint:errcheck // read only, Close error doesn't matter
return count(f, freq)
}
EOF
go build -o bin/wc ./cmd/wc
printf 'Grüße aus Hamburg!\nGo ist schnell, Go ist einfach.\n' > gruss.txt
./bin/wc gruss.txt
./bin/wc -l -w gruss.txt
./bin/wc -m gruss.txt
./bin/wc -top 3 gruss.txtExpected:
2 9 53 gruss.txt
2 9 gruss.txt
51 gruss.txt
2 9 53 gruss.txt
1. go 2
2. ist 2
3. aus 1
Why? Without -top, freq stays a nil
map: reading it and range over it are allowed,
writing would panic – that is why count checks for
nil. Because maps are references, the same map collects
words across all files. defer f.Close() closes the file as
soon as countFile returns; the nolint comment
tells the linter that a Close error doesn’t matter when only
reading.
Save your progress? Sign up for free
Drill 5: Up against the original
A clone is only good if it counts the same. Test text: Goethe’s Faust from Project Gutenberg, with umlauts and about 200 KB.
curl -sO https://www.gutenberg.org/cache/epub/2229/pg2229.txt
for f in "" -l -w -c -m; do
if diff <(./bin/wc $f pg2229.txt) <(wc $f pg2229.txt) >/dev/null; then
echo "✔ gleich: wc $f"
else
echo "✘ anders: wc $f"
fi
done
./bin/wc pg2229.txtExpected:
✔ gleich: wc
✔ gleich: wc -l
✔ gleich: wc -w
✔ gleich: wc -c
✔ gleich: wc -m
7542 33705 218440 pg2229.txt
Why? 218440 bytes but only 214638 characters – the difference is the
umlauts. wc -m counts characters instead of bytes only if
your shell has a UTF-8 locale (echo $LANG, the normal case
on a Mac); your Go tool always counts runes.
Save your progress? Sign up for free
Drill 6: stdin, errors and a flag trap
cat gruss.txt | ./bin/wc -w
./bin/wc gruss.txt nix.txt go.mod; echo "exit=$?"
./bin/wc gruss.txt -l; echo "exit=$?"
./bin/wc -top x gruss.txt 2>&1 | head -1Expected:
9
2 9 53 gruss.txt
wc: open nix.txt: no such file or directory
3 4 33 go.mod
5 13 86 total
exit=1
2 9 53 gruss.txt
wc: open -l: no such file or directory
2 9 53 total
exit=1
invalid value "x" for flag -top: parse error
Why? An error with one file does not abort everything: a message on
stderr, on to the next one, exit code 1 at the end – like the original.
Without files, wc reads from stdin, which is why it works
in pipes.
Warning: The
flagpackage stops at the first non-flag. Inwc gruss.txt -l,-lis a file name. Flags go before the arguments; only cobra (Day 5) allows both.
Save your progress? Sign up for free
Drill 7: The tool is done
cd ~/go-lab/tag01-wc
go vet ./... && ~/go-lab/bin/lint run ./...
go build -o bin/wc ./cmd/wc
./bin/wc -top 5 pg2229.txtExpected:
0 issues.
7542 33705 218440 pg2229.txt
1. und 919
2. ich 690
3. die 667
4. der 609
5. nicht 423
Your module:
tag01-wc/
├── cmd/wc/
│ ├── count.go counting (bufio, runes, closure)
│ ├── main.go flags, files, output
│ └── top.go map → slice → sorted
├── spielwiese/main.go
└── go.mod
Three files, one package: everything with package main
in the same folder belongs together and can see each other – including
lowercase names. Day 2 shows how to cut code into packages of your
own.
Save your progress? Sign up for free
Checkpoint
#!/usr/bin/env bash
check() { if eval "$2" >/dev/null 2>&1; then echo "✔ $1"; else echo "✘ $1"; fi; }
cd ~/go-lab/tag01-wc
check "gofmt sauber" "test -z \"\$(gofmt -l .)\""
check "go vet" "go vet ./..."
check "golangci-lint" "~/go-lab/bin/lint run ./..."
check "Zeilen wie wc" "diff <(./bin/wc -l pg2229.txt) <(wc -l pg2229.txt)"
check "Zeichen wie wc -m" "diff <(./bin/wc -m pg2229.txt) <(wc -m pg2229.txt)"
check "stdin funktioniert" "test \"\$(printf 'a b\n' | ./bin/wc -w | tr -d ' ')\" = 2"
check "Exit 1 bei fehlender Datei" "./bin/wc nix.txt; test \$? -eq 1"Expected:
✔ gofmt sauber
✔ go vet
✔ golangci-lint
✔ Zeilen wie wc
✔ Zeichen wie wc -m
✔ stdin funktioniert
✔ Exit 1 bei fehlender Datei
Challenge
The top list is full of filler words like “und”, “ich”, “die” (German
for “and”, “I”, “the”). In -top, allow only words with at
least four characters.
Hint: len("für") is 4 – that would be
wrong; the unicode/utf8 package counts runes.
Solution
cat > cmd/wc/top.go <<'EOF'
package main
import (
"cmp"
"slices"
"unicode/utf8"
)
// minRunes: shorter words (und, der, ich) don't count for the top list.
const minRunes = 4
// wordCount is a pair of word and frequency.
type wordCount struct {
word string
n int
}
// topWords returns the n most frequent words, alphabetically on a tie.
func topWords(freq map[string]int, n int) []wordCount {
list := make([]wordCount, 0, len(freq))
for w, k := range freq {
if utf8.RuneCountInString(w) < minRunes {
continue // not len(w): "für" has 4 bytes but 3 runes
}
list = append(list, wordCount{w, k})
}
slices.SortFunc(list, func(a, b wordCount) int {
if c := cmp.Compare(b.n, a.n); c != 0 {
return c // descending by frequency
}
return cmp.Compare(a.word, b.word)
})
if len(list) > n {
list = list[:n]
}
return list
}
EOF
go build -o bin/wc ./cmd/wc && ./bin/wc -top 5 pg2229.txtExpected:
7542 33705 218440 pg2229.txt
1. nicht 423
2. mephistopheles 283
3. faust 270
4. sich 262
5. mich 236
Mephistopheles appears in the text more often than Faust – the
speaker labels before each line of dialog are counted too. With Bash
built-ins this would be a pipeline of tr, sort
and uniq -c. The filter deliberately lives in
topWords and not in count: the -w
counts stay identical to the original.
Save your progress? Sign up for free
Enterprise Note
- Large files:
bufio.Readerreads in 4 KB blocks, so memory use stays constant – even for logs of many GB.bufio.Scanner, on the other hand, has a line limit of 64 KB (Day 2). - Encodings: Go assumes UTF-8. Convert Windows files in UTF-16 or
Latin-1 with
golang.org/x/text/encodingfirst, otherwise you countRuneErrorcharacters. - Locale differences: GNU
wcon Linux servers and BSDwcon the Mac format columns differently – never have scripts check for exact spaces. - A binary of your own does not replace the coreutils, but it runs identically on Mac, Linux and in a distroless container (Day 14).