Golang core language: functions, slices, maps, interfaces
Functions, errors as values, the two data structures you use all day, your own types, and tests.
- 01Functions, multiple returns, and errorsThere are no exceptions. An error is a value you return, and that changes everything.Graded challenge
- 02deferCleanup you cannot forget, and the two traps that catch most people once.
- 03Arrays and slicesThe data structure you use all day, and the shared-memory trap inside it.Graded challenge
- 04MapsA hash table with two behaviours that surprise most people once.Graded challenge
- 05Strings, bytes, and runesWhy `len("héllo")` is 6, and the string operations you will use every day.Graded challenge
- 06PointersWhat `&` and `*` do, when you need them, and why Go pointers cannot hurt you the way C ones can.
- 07StructsHow you make your own types by grouping values under one name.Graded challenge
- 08Methods, receivers, and embeddingThe rule about receivers that most beginners trip on.Graded challenge
- 09InterfacesGo’s main abstraction, satisfied without ever being declared.Graded challenge
- 10Type assertions and type switchesGetting the concrete value back out of an interface, safely.Graded challenge
- 11Error values in depthSentinels, custom types, wrapping, and handling each error once.Graded challenge
- 12JSONTurning structs into JSON and back, and the field that disappears with no error.Graded challenge
- 13TestingPart of the toolchain, not a library you pick.
What each lesson covers
By the end of this section you will be able to do everything below. Each lesson has runnable Go examples you can edit and run in the browser.
1. Functions, multiple returns, and errors
- Write a function that returns a result and an
error, and check the error at the call site. - Explain why Go returns errors as values instead of throwing exceptions.
- Write a variadic function and a closure that keeps its own state.
Remember: An error is a value, not an event. Every failure point is visible in the code you are reading.
- Write a function that returns a result and an
2. defer
- Predict the order in which several deferred calls run.
- Explain why
defer fmt.Println(x)prints the valuexhad on the defer line. - Fix a loop that defers
Closeso each file is closed on time.
Remember: defer puts cleanup next to the thing that needs cleaning up, and runs it on every return path.
3. Arrays and slices
- Explain the difference between a slice's length and its capacity.
- Predict the output when you write through a sub-slice.
- Fix a shared-memory bug with
copyor a full slice expressions[a:b:b].
Remember: A slice is a window onto an array, not the array itself. Two windows see each other’s writes.
4. Maps
- Write code that tells a missing key from a zero value with
v, ok := m[k]. - Explain why ranging over a map may give a different order each time.
- Fix the panic caused by writing to a nil map.
Remember: Absent and zero are different things. The two-value form is how you tell them apart.
- Write code that tells a missing key from a zero value with
5. Strings, bytes, and runes
- Explain why
len("héllo")is 6 and not 5. - Write a function that works per character with
[]runeorrangeinstead of bytes. - Fix a string-building loop that uses
+=by switching tostrings.Builderorstrings.Join.
Remember: A string is bytes, not characters. The moment you index or slice one, ask yourself whether you meant a byte or a character.
- Explain why
6. Pointers
- Predict whether a function call changes the caller's value when it takes a value versus a pointer.
- Write a function that changes its argument through a pointer.
- Explain how a nil
*inttells "no value" apart from zero.
Remember: A pointer is a value holding an address. Go removes the dangerous parts (arithmetic and manual freeing) and keeps the useful part.
7. Structs
- Write a struct type and create values of it with named fields.
- Predict the output after copying a struct and changing the copy.
- Write a function that changes a struct through a pointer.
Remember: Everything in Go is a value and every assignment is a copy.
8. Methods, receivers, and embedding
- Write a value-receiver method and a pointer-receiver method, and explain when to use each.
- Write an enum with a named type,
iotaand aString()method. - Explain why embedding is composition and not inheritance.
Remember: If one method needs a pointer receiver, make them all pointer receivers.
9. Interfaces
- Write an interface and two types that satisfy it without declaring it.
- Explain why an interface holding a nil pointer is not
nil. - Fix a function that returns a typed nil pointer as an
error.
Remember: Do not create an interface until there is a second implementation or a test that needs a fake.
10. Type assertions and type switches
- Write a comma-ok type assertion that never panics.
- Write a type switch that handles several concrete types and a default.
- Fix a type switch where a broad interface case hides a more specific one.
Remember: Comma-ok unless a wrong type is a bug.
11. Error values in depth
- Write a sentinel error and a custom error type, and wrap them with
%w. - Write checks with
errors.Isfor a specific error anderrors.Asfor its data. - Explain the difference between wrapping with
%wand with%v.
Remember: Add context, do not restate. Say what you were doing, with what.
- Write a sentinel error and a custom error type, and wrap them with
12. JSON
- Write struct tags that rename, drop and hide fields in JSON.
- Explain why a lowercase field never appears in the output.
- Predict which fields
omitemptyandomitzeroleave out.
Remember: If a field is missing from your API response, check its first letter.
13. Testing
- Write a table-driven test with named subtests and clear failure messages.
- Write a fuzz test and a
b.Loopbenchmark, and run them withgo test. - Explain the difference between
t.Errorandt.Fatal.
Remember: There is nothing to install and nothing to configure. Testing ships with the language.
Test yourself
- Core language · 18 questionsErrors, slices, strings, pointers, structs, interfaces, packages and tests.
- Checkpoint 1 · 10 questionsGetting started plus the first half of Core language: functions, defer, slices, maps, strings, pointers and structs.
- Checkpoint 2 · 10 questionsAll of Getting started and Core language mixed together, through interfaces, errors, packages, JSON and testing.