From the first program to a service-level Middle
A dense trail through modern Go. First the language becomes predictable: types, slices, methods, interfaces, and errors. Then concurrency, tests, and a small HTTP service appear — the things a Middle developer works with every week.
- You read someone else's Go and see where a value is, and where a pointer, an interface, or a race is
- You write packages with proper errors, tests, and boundaries
- You bring up a small HTTP service with context, middleware, and shutdown
Foundations
The module, the package, formatting, and how a Go program is actually built.
- The module and the first program — What a module is, why go.mod exists, and why you run a program as a package rather than as a single file.
- gofmt, vet, and documentation — Formatting is not debated in Go, and go vet catches suspicious code before review.
- Packages, export, and internal — Visibility in Go is decided by the case of a letter, and the internal directory closes the API to other modules.
Types and values
Zero values, numbers, and strings as bytes and runes. Without this, the rest of the language is read by guesswork.
- Variables, constants, and zeros — Declaration, short assignment, and what Go puts in memory when you did not set a value.
- Numbers and conversions — Integers do not turn into each other silently, and overflow is not caught by a panic.
- Strings, bytes, and runes — A string is an immutable sequence of bytes. Length and "the number of letters" match only for ASCII.
Control flow
Branches, the single loop, and defer. This is also where the loop variable lives, and since Go 1.22 it behaves differently from what older articles describe.
- if and switch — Conditions without parentheses, a switch that does not fall through, and a short scope for a variable.
- for, range, and the loop variable — Go has one loop. Since 1.22 each iteration has its own variable — older articles are wrong about this.
- defer — Deferred calls are taken off a stack. They can still change a named result. An ordinary result is already copied.
Functions
Multiple results, closures, and functions as ordinary values. This is how Go passes behavior without classes.
- Results and errors side by side — A function calmly returns several values. The error is the last of them, not an exception.
- Closures — A function inside a function remembers the variables around it. That is state without an object.
- Functions as values — Behavior is passed as an argument. That is how handlers, comparators, and strategies work without a class hierarchy.
Slices and maps
How Go's main collections are built, where they share memory, and why you cannot walk a map while counting on order.
- The slice, len, and cap — A slice is a header over an array. append sometimes writes into that same array, and sometimes allocates a new one.
- Maps — Reading a missing key is safe. Writing to a nil map is not. The walk order does not belong to you.
- Copies and independence — A slice after a slice expression still looks at someone else's array. Independence has to be made explicit, with copy or slices.Clone.
Pointers, structs, methods
What gets copied when you pass a value, when a pointer is needed, and how a value receiver differs from a pointer receiver.
- Pointers — A pointer is the address of a value. Copying the argument does not change it. A write through the address does.
- Structs — A struct gathers fields into a value. Tags are for encoders. Embedding is not inheritance.
- Methods and receivers — A method is a function with a receiver. The method set of a value and of a pointer differ.
Interfaces
An implementation is nowhere declared. A small interface on the caller's side matters more than a thick interface "for every case".
- Implicit implementation — A type implements an interface if it has the required methods. The word implements does not exist in the language.
- Type assertion and nil — A concrete type is taken out of an interface explicitly. A nil pointer inside an interface is no longer a nil interface.
- Where to declare an interface — Accept interfaces, return concrete types. An interface lives with the code that uses it.
Errors
An error is a value. You wrap it, compare it with Is and As, and almost never replace it with a panic.
- Errors as values — error is an interface with an Error method. You check it immediately, rather than saving it until the end of the function.
- Wrapping, Is, and As — "%w" keeps the chain. Is looks for a sentinel. As pulls out a concrete type.
- panic and recover — A panic cuts off the normal flow. It does not replace input checks, and you do not catch everything in every function.
Generics
Type parameters remove copy-paste when the algorithm is one and the types are different. They do not replace interfaces.
- Type parameters — A function can be shared across types without losing the check at compile time.
- Constraints — any allows everything and therefore forbids operators. Comparison and order require a constraint.
- When a generic is not needed — One concrete type, and an interface with behavior, are almost always clearer than a type parameter.
Concurrency
A goroutine, a channel, and select. It is too early to parallelize everything, but forgetting to wait for completion is already a Middle concern.
- Goroutines — go starts a function concurrently. It is not an operating-system thread, and it is not background magic that waits for the end by itself.
- Channels — A channel passes a value and the fact "I am finished". The sender closes it. The reader leaves range by itself.
- select — select waits on several channels at once. default makes waiting optional, and an empty default easily becomes a hot loop.
Shared memory and cancellation
A mutex, a race, and context. The Middle distinction here is simple: you can see who owns the state.
- Mutex, WaitGroup, Once — When state is shared, it is covered by a lock. A mutex must not be copied.
- Data races — A race is simultaneous access to memory where at least one access writes. You look for it with the race detector, not with luck on a stand.
- context — A context carries cancellation and a deadline. Functions that may wait take it as the first argument.
Testing
Table tests, benchmarks, and the seams through which time, the network, and the database are replaced.
- Table tests — One test, many cases. The name of the case is visible in the output when exactly that case fails.
- Benchmarks and coverage — A benchmark measures what you repeated. Coverage shows which code ran, not which code is correct.
- Seams and stubs — A test replaces behavior at a boundary. For that the boundary has to be an interface or a function, not a database call straight from the handler.
The standard library
JSON, HTTP, time, and slog — the pieces an ordinary service is assembled from, without a framework.
- encoding/json — Tags decide field names. An unknown field is silently skipped by default, and a parse error must not be hidden.
- net/http — A handler is a function. A server without timeouts is unsafe to run, even if it is a teaching server.
- Time, io, and slog — Durations are parsed by time.ParseDuration. Logs in a service are structured, through log/slog.
A small service
How to put packages, handlers, and process shutdown together so the service can still be read six months later.
- Package boundaries — cmd starts the process, internal hides the details, and dependencies go in one direction.
- Handlers and API errors — The status is part of the contract. The error text for the client is short. The chain for the journal is complete.
- Config and shutdown — A process must be able to finish: hear the signal, stop accepting new work, and close the database.