Learn Go

Simple, fast compiled language for servers, tools and concurrent systems

39 lessons · 8 hours · 4 levels · covers Go 1.23

Go is a statically typed, compiled language from Google with a small spec, fast builds, garbage collection and first-class concurrency via goroutines and channels. It powers cloud infrastructure (Docker, Kubernetes, Terraform), web services and command-line tools. Learn it to build reliable, efficient networked software that is easy to read and deploy as a single binary.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics11 lessons · 2 hours in total
2. PracticalUse it productively at work, with the usual idioms25 lessons · 5 hours in total
3. AdvancedThe less common features, performance and trade-offs33 lessons · 6 hours in total
4. ExpertInternals and the edge cases experts know39 lessons · 8 hours in total

The Go tree

GoRunning code with the go toolRunning code withthe go toolPackages, imports and mainPackages, importsand mainVariables and basic typesVariables and basictypesConstants and iotaConstants and iotaif, for and switchif, for and switchFunctions, multiple returns, closuresFunctions, multiplereturns, closuresArrays and slicesArrays and slicesMapsMapsStrings, bytes and runesStrings, bytes andrunesStructsStructsPointersPointersSlice internals: append and aliasingSlice internals:append and aliasingMethods and receiversMethods andreceiversInterfacesInterfacesErrors and wrappingErrors and wrappingdefer, panic and recoverdefer, panic andrecoverModules and go.modModules and go.modTesting and table-driven testsTesting andtable-driven testsGoroutinesGoroutinesChannelsChannelsselect and timeoutsselect and timeoutssync.WaitGroup and Mutexsync.WaitGroup andMutexcontext: cancellation and deadlinescontext: cancellationand deadlinesJSON and struct tagsJSON and struct tagsHTTP servers and clientsHTTP servers andclients
Essentials and Practical shown (25 of 39 lessons); the full list is below. Click a lesson to read it.

All 39 lessons, in order

Getting started

  1. Running code with the go tool · Essentials · 8 min

    Install Go, write hello world, and use go run, go build, go fmt and go vet.

  2. Packages, imports and main · Essentials · 8 min

    How Go code is split into packages, how imports work, and what makes a program executable.

  3. Variables and basic types · Essentials · 10 min

    Declare variables with var and :=, meet Go's basic types, zero values and explicit conversions.

  4. Constants and iota · Essentials · 7 min

    Declare compile-time constants, use untyped constants, and build enumerations with iota.

  5. if, for and switch · Essentials · 10 min

    Go's three control structures: if with an init statement, the single for loop, and switch.

  6. Functions, multiple returns, closures · Essentials · 12 min

    Declare functions, return several values, use variadic parameters, and treat functions as values.

Data types

  1. Arrays and slices · Essentials · 12 min

    Fixed-size arrays, the dynamic slices built on them, and how len, cap, append and slicing behave.

  2. Maps · Essentials · 10 min

    Create, read, update and delete map entries, test for presence with comma-ok, and iterate.

  3. Strings, bytes and runes · Essentials · 12 min

    Strings as immutable UTF-8 bytes, runes as code points, and the strings and strconv packages.

  4. Structs · Essentials · 10 min

    Group fields into struct types, create values with composite literals, and compare and copy them.

  5. Pointers · Essentials · 10 min

    Take addresses with &, dereference with *, and use pointers to share and mutate values.

  6. Slice internals: append and aliasing · Practical · 12 min

    How append grows the backing array, when two slices alias each other, and how to avoid surprises.

Methods and interfaces

  1. Methods and receivers · Practical · 10 min

    Attach methods to your types and choose between value and pointer receivers.

  2. Interfaces · Practical · 14 min

    Describe behaviour as a set of methods, satisfied implicitly; type assertions, type switches and any.

  3. Embedding and composition · Advanced · 12 min

    Compose types by embedding structs and interfaces, and understand method promotion and its limits.

Errors, modules and testing

  1. Errors and wrapping · Practical · 14 min

    Return errors as values, add context with %w, and inspect them with errors.Is and errors.As.

  2. defer, panic and recover · Practical · 10 min

    Schedule cleanup with defer, understand panics, and recover from them at boundaries.

  3. Modules and go.mod · Practical · 12 min

    Manage dependencies with go.mod and go.sum, go get, go mod tidy, versions and workspaces.

  4. Testing and table-driven tests · Practical · 14 min

    Write tests with the testing package, organise them as tables with subtests, and run them with go test.

Concurrency

  1. Goroutines · Practical · 10 min

    Start concurrent work with the go keyword and understand what goroutines are and cost.

  2. Channels · Practical · 14 min

    Send and receive values between goroutines with unbuffered and buffered channels, close and range.

  3. select and timeouts · Practical · 10 min

    Wait on several channel operations at once, add timeouts, and do non-blocking sends and receives.

  4. sync.WaitGroup and Mutex · Practical · 12 min

    Wait for a group of goroutines, protect shared state with Mutex and RWMutex, and run setup once.

  5. context: cancellation and deadlines · Practical · 12 min

    Propagate cancellation, timeouts and request-scoped values through call chains with context.Context.

  6. Concurrency patterns: pipelines and fan-out · Advanced · 15 min

    Compose goroutines and channels into pipelines, fan-out/fan-in, and cancellable stages with errgroup.

  7. Worker pools and rate limiting · Advanced · 12 min

    Bound concurrency with a fixed pool of workers reading a job channel, collect results and errors, and pace work.

JSON and HTTP

  1. JSON and struct tags · Practical · 12 min

    Encode and decode JSON with encoding/json, control field names with struct tags, and stream data.

  2. HTTP servers and clients · Practical · 15 min

    Build an HTTP API with net/http's ServeMux routing patterns and call services with a configured client.

Generics, iterators and reflection

  1. Generics: type parameters · Advanced · 15 min

    Write functions and types with type parameters, use constraints like comparable and cmp.Ordered.

  2. Range-over-func iterators · Advanced · 14 min

    Write and consume Go 1.23 iterators: iter.Seq, iter.Seq2, yield, and the iterator helpers in slices and maps.

  3. Reflection with reflect · Advanced · 14 min

    Inspect types and values at run time with reflect.TypeOf and ValueOf, read struct tags, and set fields.

Correctness and performance

  1. Data races and the race detector · Advanced · 10 min

    What a data race is, why it's undefined behaviour in practice, and how go test -race finds them.

  2. Benchmarks and pprof profiling · Advanced · 15 min

    Measure performance with testing benchmarks and benchstat, then find hot spots with CPU and heap profiles.

Runtime internals

  1. The scheduler: G, M and P · Expert · 15 min

    How the Go runtime multiplexes goroutines onto OS threads: run queues, work stealing, syscalls, preemption.

  2. The Go memory model · Expert · 15 min

    Happens-before, what synchronisation guarantees visibility, and why racy programs have no reliable meaning.

  3. Escape analysis and allocation · Expert · 15 min

    How the compiler decides stack vs heap, reading -gcflags=-m output, and techniques to cut allocations.

  4. The garbage collector and GOGC · Expert · 15 min

    How Go's concurrent mark-sweep GC works, the GOGC and GOMEMLIMIT knobs, and how to observe and tune it.

  5. Interface internals: itabs and nil · Expert · 14 min

    How interface values are represented (itab/type + data), the cost of boxing and dynamic dispatch, and the typed-nil trap.

  6. unsafe and cgo · Expert · 15 min

    Step outside the type system with unsafe.Pointer, follow its rules, and call C code with cgo at a known cost.