Skip to content

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

  1. Try it yourself first — rename the solution aside and re-implement from the prompt below.
  2. Run go test ./... from this directory.
  3. 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 tiny wc). The last line counts even without a trailing newline; surface non-EOF read errors. Hint: tee the reader through a byte-counting io.Writer so bufio.Scanner stripping 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 return ErrRaggedRows on a width mismatch.
  • slugify: implement Slug(s string) string that lowercases, keeps ASCII letters/digits, collapses every other run into a single -, and trims edge dashes. Build it with strings.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 ./....


Exercises