Stage 1 · Code
Functions & Composite Types
Data Modeling Practice
Build a small contact list application that puts slices, maps, structs, and functions together into a working program.
Design the Model
A contact list needs to store people, look them up by name or email, and list all entries. We will model this with a struct for a contact, a slice for ordering, and a map for fast lookup.
Add and Lookup
Adding a contact inserts it into the slice and records its index in the lookup map. Duplicate emails are an error — the comma-ok idiom detects them in constant time.
Listing and Filtering
A List method returns all contacts in insertion order. A FindByTag method shows how to filter with a higher-order function — accept a predicate and apply it to every contact.
Table-Driven Examples
Table-driven tests are the Go standard for verifying behaviour across many input/output pairs. Each row of the table is one scenario — the test loop runs them all automatically.
The AddressBook combines every composite type from this module: a struct to model a contact, a slice to maintain insertion order, a map for O(1) lookup, and functions to add, retrieve, filter, and test behaviour. This is the Go way of building domain models — simple types, composed.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.