Learn Go from Python¶
A personal, opinionated set of conspect notes for a Python developer picking up Go. The goal is to explain Go on its own terms, with short Python analogies thrown in only when they sharpen the contrast.
All material targets the current stable Go release, Go 1.27.1.
How the notes are organised¶
Each topic is a numbered folder. Inside each folder, conspects and runnable code examples share one numeric sequence so the reading order is always obvious.
Ecosystem and installation¶
- What is Go — the language, the community, the ecosystem.
- The
gocommand — every subcommand you'll touch. go tool trace— the execution tracer.- File types —
.go,_test.go,go.mod,go.sum, build constraints. - Special folders —
internal/,testdata/, conventions. - Multiple Go versions —
GOTOOLCHAIN, thegoandtoolchaindirectives. - Installation — macOS, Linux, Windows.
- Additional tools —
gopls,dlv,golangci-lint, and friends. - Demo project — a small runnable module that exercises the above.
Language basics¶
- Variables and constants —
var,:=,const, andiota. - Basic types — integers, floats, strings, booleans; no truthiness.
- Type conversions — explicit
T(x),strconv, no implicit coercion. - Operators — arithmetic, overflow, integer division; no ternary.
- Control flow —
if,for(the only loop),switch. - Functions — multiple returns, named results, variadics, first-class values.
- Errors — the
errorvalue, wrapping with%w,errors.Is/As. - Pointers —
&/*,nil,new, no pointer arithmetic. - Custom types —
typedefinitions vs aliases, underlying types. - Structs — fields, literals, zero value, embedding, tags.
- Arrays and slices — len/cap,
append, and the shared-backing gotcha. - Maps — keyed lookup, comma-ok, the nil-map trap, sets.
- Choosing a data structure — slice vs map vs struct vs custom type.
- Defer — deferred calls, LIFO order, cleanup patterns.
- Panic and recover — when to panic, recovering in deferred calls.
- Imports — import paths, aliases, blank and dot imports.
Object-oriented Go¶
- Methods — value vs pointer receivers, method sets, promotion.
- Interfaces — implicit satisfaction, polymorphism, the empty interface /
any. - Type assertions and type switches — recovering the concrete type at runtime.
- Generics — type parameters and constraints.
- OOP patterns — encapsulation, composition over inheritance, polymorphism.
- Custom error types — your own
errortypes,Unwrap,errors.As, customIs.
Packages and modules¶
- Packages and visibility — package rules, exported vs unexported,
init. - Creating and publishing a module —
go.mod, versioning,replace, publishing. - Project layout and workspaces —
internal/,cmd/,go.work.
Concurrency¶
- Goroutines —
go, scheduling,WaitGroup, the main-exits trap. - Channels — send/receive, buffering,
close,range, deadlocks. - select — multiplexing,
default, timeouts, done-channels. - Synchronization —
Mutex,Once, atomics, the race detector. - Context — cancellation, deadlines, propagation.
- Concurrency patterns — worker pools, fan-out/fan-in, pipelines.
- Bounded concurrency — channel semaphores, collecting results,
sync.Map. - Long-running goroutines — per-goroutine
recover, tickers, draining on shutdown.
Text, time and data¶
- Strings, bytes and runes —
strings,bytes, and whylencounts bytes. - Formatting with
fmt— the verbs, width and precision,Stringer,%w. - Regular expressions —
regexp, named groups, and what RE2 leaves out. - Time — the reference layout, durations, zones,
Equalover==. - Sorting —
slices.SortFunc,cmp.Compare,cmp.Or, stability. - Iterators — writing
iter.Seq, theyieldcontract,iter.Pull. - Encoding JSON — tags,
omitempty,RawMessage, custom marshalling. - XML, CSV and reflection — token streaming,
csv, struct tags at runtime.
The operating system¶
- Files and paths —
os,filepath,WalkDir, testing errors not paths. - Readers and writers —
io.Copy,bufio.Scanner, and its 64 KB limit. go:embed— files in the binary,embed.FS,all:,fs.Sub.- Flags and environment —
flag, subcommands,LookupEnv, exit codes. - Running external commands —
exec.CommandContext,ExitError, no shell. - Signals and graceful shutdown —
NotifyContext, draining on a budget. - Hashing and random values — sha256, HMAC,
crypto/rand, base64, gzip.
HTTP with net/http¶
- An HTTP server — handlers,
ServeMuxrouting, timeouts, shutdown. - An HTTP client — why a 404 is not an error, closing bodies, retries.
- Middleware — wrapping handlers, recovery, context values.
- Templates —
text/templatevshtml/templateand contextual escaping. - Server-sent events — streaming, flushing, dropping slow clients.
Databases with database/sql¶
database/sql— the pool,Scan,ErrNoRows, NULL,rows.Err().- Custom column types —
driver.Valuerandsql.Scanner. - Transactions — the closure wrapper, rollback on panic, nesting.
- The repository pattern — a contract package, translating storage errors.
Testing¶
- The
testingpackage —TestXxx,ErrorfvsFatalf,go testflags. - Table-driven tests — the case slice,
t.Run,t.Parallel. - Helpers, fixtures and golden files —
t.Helper,t.TempDir,testdata/. - Fakes and stubs — function-field doubles, faking the clock.
- Testing HTTP —
httptestrecorders and servers. - Benchmarks, fuzzing and the race detector —
b.Loop,f.Fuzz,-race.
Architecture and conventions¶
- Wiring and package structure —
cmd/,internal/, constructor injection. - Context as a carrier — unexported keys,
WithoutCancel, what not to put in. - Configuration patterns — one struct, defaults, validation with
errors.Join. - Dependency direction — which package may import which, and why.
- Build, code generation and cgo — build tags,
-ldflags, cross-compiling, cgo's cost. - Project conventions — error wrapping, log levels, naming, comments.
Observability¶
- Structured logging with
slog— handlers,With,LogValuer, testing logs. - Profiling with pprof — CPU and heap profiles, flat vs cum, flame graphs.
Third-party libraries¶
Everything that lands in go.mod. The rest of the book is the language and
its standard library; this section is one stack's worth of choices.
- Choosing and managing dependencies — judging a module,
go mod tidy,GOPRIVATE,govulncheck. - golangci-lint configuration —
depguard,forbidigo, enforcing architecture. golang.org/x/syncandx/time—errgroup,SetLimit, rate limiters.- YAML and TOML —
yaml.v3,go-toml, strict decoding. - viper — layered config, and why
AutomaticEnvalone is not enough. - pgx and PostgreSQL — native types,
PgError,CollectRows, UUIDv7. - GORM basics — models, tags, hooks, and the zero-value trap.
- GORM queries and transactions — chaining, raw SQL, context-carried transactions.
- goose migrations — SQL migrations, embedding, safe schema changes.
- Fiber — v3 handlers, binding, and what fasthttp costs.
- templ — compiled, type-checked HTML components.
- testify —
assertvsrequire, diffs, nil vs empty. - testcontainers — a real database per test binary.
- Prometheus and OpenTelemetry — metrics, traces, cardinality.
- Scheduled jobs — gocron plus advisory locks for exactly-once.
- Object storage and caching — S3, valkey, and failing soft.
- OIDC and OAuth — the code flow, PKCE, sessions, API tokens.
- MCP servers with mcp-go — tools, handlers, transports.
Source¶
- Source repository: https://github.com/oduvan/learn-go-from-python.
- Each conspect cites the official sources it consulted at the bottom of the page — typically go.dev, pkg.go.dev, or the Go specification.