Dev Skills
Go·Section 2 of 15 · 13 lessons · about 10 hours

Golang core language: functions, slices, maps, interfaces

Functions, errors as values, the two data structures you use all day, your own types, and tests.

  1. 01Functions, multiple returns, and errors
    There are no exceptions. An error is a value you return, and that changes everything.
    Graded challenge
  2. 02defer
    Cleanup you cannot forget, and the two traps that catch most people once.
  3. 03Arrays and slices
    The data structure you use all day, and the shared-memory trap inside it.
    Graded challenge
  4. 04Maps
    A hash table with two behaviours that surprise most people once.
    Graded challenge
  5. 05Strings, bytes, and runes
    Why `len("héllo")` is 6, and the string operations you will use every day.
    Graded challenge
  6. 06Pointers
    What `&` and `*` do, when you need them, and why Go pointers cannot hurt you the way C ones can.
  7. 07Structs
    How you make your own types by grouping values under one name.
    Graded challenge
  8. 08Methods, receivers, and embedding
    The rule about receivers that most beginners trip on.
    Graded challenge
  9. 09Interfaces
    Go’s main abstraction, satisfied without ever being declared.
    Graded challenge
  10. 10Type assertions and type switches
    Getting the concrete value back out of an interface, safely.
    Graded challenge
  11. 11Error values in depth
    Sentinels, custom types, wrapping, and handling each error once.
    Graded challenge
  12. 12JSON
    Turning structs into JSON and back, and the field that disappears with no error.
    Graded challenge
  13. 13Testing
    Part 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. 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.

  2. 2. defer

    • Predict the order in which several deferred calls run.
    • Explain why defer fmt.Println(x) prints the value x had on the defer line.
    • Fix a loop that defers Close so 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. 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 copy or a full slice expression s[a:b:b].

    Remember: A slice is a window onto an array, not the array itself. Two windows see each other’s writes.

  4. 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.

  5. 5. Strings, bytes, and runes

    • Explain why len("héllo") is 6 and not 5.
    • Write a function that works per character with []rune or range instead of bytes.
    • Fix a string-building loop that uses += by switching to strings.Builder or strings.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.

  6. 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 *int tells "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. 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. 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, iota and a String() method.
    • Explain why embedding is composition and not inheritance.

    Remember: If one method needs a pointer receiver, make them all pointer receivers.

  9. 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. 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. 11. Error values in depth

    • Write a sentinel error and a custom error type, and wrap them with %w.
    • Write checks with errors.Is for a specific error and errors.As for its data.
    • Explain the difference between wrapping with %w and with %v.

    Remember: Add context, do not restate. Say what you were doing, with what.

  12. 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 omitempty and omitzero leave out.

    Remember: If a field is missing from your API response, check its first letter.

  13. 13. Testing

    • Write a table-driven test with named subtests and clear failure messages.
    • Write a fuzz test and a b.Loop benchmark, and run them with go test.
    • Explain the difference between t.Error and t.Fatal.

    Remember: There is nothing to install and nothing to configure. Testing ships with the language.

Test yourself