Level 2: Intermediate Go Exercises
Level 2 is about the parts of Go that make backend code safe to change: types with methods, small interfaces, and errors that carry context. You build a wallet, send alerts through several channels, wrap "not found" errors, keep one panicking handler from crashing a server, and finish with an in-memory session store.
The traps here are the ones that reach production: a value receiver that
updates a copy, an unset status that silently means "pending", comparing a
wrapped error with ==, or a struct copy that still shares its slice. Each
one has a test that only the correct version passes.
The 10 exercises in this level
- 1 Methods and Pointer Receivers A wallet that keeps its balance Give a Go struct methods that change it, and see why a value receiver silently updates a copy while a pointer receiver updates the real wallet. Start here — no prerequisites
- 2 Interfaces Send one alert through email and Slack Define a Notifier interface in Go and send one alert through email and Slack: any type with the right method satisfies an interface, with no implements keyword. Recommended first: Methods and Pointer Receivers
- 3 Wrapping Errors Add context without losing the cause Wrap a sentinel error with fmt.Errorf and %w in Go to add context like the user ID, and check it with errors.Is, because == no longer matches a wrapped error. Recommended first: Methods and Pointer Receivers
- 4 Constants and iota Order statuses that can't be mistaken Model order statuses as a Go enum with const and iota, give them a String method, and keep the zero value for Unknown so an unset status never reads as a real one. Recommended first: Methods and Pointer Receivers
- 5 Defer and Recover Keep one bad request from crashing the server Catch a panicking HTTP handler with defer and recover in Go and turn it into an error, so one bad request can't crash the whole server. Recommended first: Wrapping Errors
- 6 Closures Count requests per user Return a function that remembers its own state in Go: a closure that counts requests per user, with every counter keeping separate counts. Recommended first: Methods and Pointer Receivers
- 7 Generics One Unique function for every type Write one generic Unique function in Go that removes duplicates from slices of any comparable type, keeping the order in which items first appear. Recommended first: Closures
- 8 Type Constraints Sum amounts whatever their number type Write a Number constraint and a generic Sum in Go that works for int, float64 and named types like Cents, using ~int so types built on int are accepted too. Recommended first: Generics
- 9 Copying Structs Duplicate an order without sharing its items Clone an order struct in Go so the copy's items can change without touching the original: assigning a struct copies a slice field's header, not its items. Recommended first: Methods and Pointer Receivers
- 10 Project: Session Store Log users in and out by token Build an in-memory session store in Go with a map and methods to set, get, delete and count sessions, using the comma-ok idiom to tell a missing token from an empty value. Recommended first: Copying Structs, Closures
Frequently asked questions
- When should a method use a pointer receiver?
- When it changes the struct, or when the struct is large. A value receiver works on a copy, so changes are lost. If any method needs a pointer receiver, give all of the type's methods pointer receivers, to stay consistent.
- Does Go have enums?
- Not as a keyword. You declare a named type such as `type Status int` and constants with `iota`. Keep 0 for an "unknown" value, because every variable you don't set starts at 0; "Constants and iota" shows why.
- Are generics worth using in Go?
- For small helpers that work on any type, like `Unique`, `Map` or `Sum`, yes. Constraints such as `comparable` and `~int | ~float64` say exactly which types are allowed, and the compiler checks every call.