Table of Contents
Month 2 · Week 1 — Exercises¶
Practice for the standard-library I/O, formatting, and string packages. Each folder is its own package with a solution and table-driven tests. Standard library only.
| Exercise | Concept | Day | Run tests |
|---|---|---|---|
linestats/ |
io.Reader, bufio.Scanner, io.TeeReader counting |
029–030 | go test ./linestats |
csvsum/ |
strings.Split, strconv.Atoi, %w error wrapping |
033 | go test ./csvsum |
slugify/ |
strings.Builder, rune classification, no += in loops |
034 | go test ./slugify |
How to use¶
- Try it yourself first — rename the solution aside and re-implement from the prompt below.
- Run
go test ./...from this directory. - Compare with the provided solution; log differences in your day note's "Mistakes" section.
Prompts¶
- linestats: implement
Count(r io.Reader) (Stats, error)returning lines, words, and exact byte counts (a tinywc). The last line counts even without a trailing newline; surface non-EOF read errors. Hint: tee the reader through a byte-countingio.Writersobufio.Scannerstripping the newline doesn't throw off the byte total. - csvsum: implement
SumColumns(r io.Reader) ([]int, error)that sums each column of comma-separated integers. Skip blank lines, wrap parse failures with%w(keep the*strconv.NumError), and returnErrRaggedRowson a width mismatch. - slugify: implement
Slug(s string) stringthat lowercases, keeps ASCII letters/digits, collapses every other run into a single-, and trims edge dashes. Build it withstrings.Builder, never+=in the loop.
Results¶
| Exercise | Tests | Status |
|---|---|---|
| linestats | TestCount, TestCountPropagatesError |
✅ |
| csvsum | TestSumColumns, TestSumColumnsRagged, TestSumColumnsBadInt |
✅ |
| slugify | TestSlug |
✅ |
Tip: run all of Week 1 at once with
go test ./....