Go tutorial
Learn Go, the simple and fast language behind Docker and Kubernetes, from your first program to goroutines.
Course contents
Go Foundations
- Introduction to Go — What Go is, why Google made it, its history, features and philosophy, where it is used, how to set it up with Go modules, and how to write your first program with comments.
- Variables and Constants — How to declare, initialize and change variables with var and :=, make constants with const and iota, name things, and ignore values with the blank identifier.
- Data Types — Go's number types, strings, booleans, bytes and runes, zero values, nil, type inference, and converting between types with T(x) and strconv.
- Operators — Arithmetic, increment and decrement, relational, logical, assignment and bitwise operators in Go, and the order in which Go works out an expression.
Control Flow
- Conditional Statements — Make choices in Go with if, if with a short statement, else, else if chains, nested if, switch, multiple cases, fallthrough and the expressionless switch.
- Loops — Repeat work in Go with the for loop in its three forms, walk through arrays, slices, strings, maps and channels with range, and control loops with break, continue, labels and goto.
- Functions — Declare and call Go functions, pass parameters, return one, many or named values, and use variadic functions, closures, higher-order functions, recursion, defer and scope.
Packages and Modules
- Packages and Modules — How Go organises code into packages, exported and unexported names, the four kinds of import, Go modules with go.mod, go.sum and go mod tidy, managing dependencies, and the key standard packages.
Strings and Time
- Strings — Create interpreted and raw strings, find the length, join, slice and compare text, use the strings package, understand runes, Unicode and UTF-8, and build text with strings.Builder.
- Time and Date — Work with time.Time and time.Duration: get the current time, build and parse dates, read their parts, add, subtract and compare times, and format them with Go's reference layout.
Collections
- Arrays — Go arrays: fixed-size lists of one type. Declare and create them, read, update and loop through elements, and learn length, zero values, copying, comparing and 2D arrays.
- Slices — Go slices: growable lists built on arrays. Create them, append, copy, len and cap, the underlying array, slicing, removing and inserting, and arrays vs slices.
- Maps — Go maps: key-value pairs. Create them with literals and make, add, update, delete and look up values, check keys with comma-ok, loop with range, and use zero values and nested maps.
Types and OOP
- Pointers — Go pointers: memory addresses, the & and * operators, changing values through pointers, nil pointers, pass by value vs pointer parameters, and struct pointers.
- Structs and Methods — Define structs, create and fill them, read, update and compare fields, add methods with value and pointer receivers, write constructors, and use anonymous structs and struct tags.
- Composition and Embedding — Build bigger types from smaller ones: has-a composition, struct embedding with promoted fields and methods, method resolution, and why Go prefers composition over inheritance.
- Interfaces and Polymorphism — Go interfaces: implicit implementation, single and multiple-method interfaces, interface values, nil interfaces, any, type assertions, type switches and polymorphism.
- Encapsulation — Hide data in Go with package-level visibility: export rules, getter methods for read access, read-only patterns, and setter methods with validation.
- Advanced Type Design — Custom types with the type keyword, type aliases, methods on any named type, method sets and their rules, and composing interfaces from smaller ones.
- Generics — Go generics: type parameters, generic functions and return types, generic structs, the any and comparable constraints, custom constraints, and practical generic helpers.
- Collections of Structs — Work with slices and maps of structs: add, update, delete and search records, sort by fields with sort.Slice, filter with custom conditions, and build a CRUD program.
Error Handling
- Error Handling — Errors as values in Go: the error interface, creating, returning and checking errors, custom error types, wrapping with %w, errors.Is and errors.As, and panic and recover.
Files and JSON
- File Handling — Create, read, write and append files with os and bufio, make and list directories, read file info and permissions, and handle missing-file and permission errors.
- JSON — JSON in Go with encoding/json: JSON structure, json.Marshal and MarshalIndent, json.Unmarshal, struct tags, nested JSON, and uses like config files and API responses.
Concurrency
- Goroutines — Concurrency in Go: concurrency vs parallelism, starting goroutines with go, waiting with WaitGroup, anonymous goroutines, what happens when main ends, and concurrent processing.
- Channels — Go channels: create, send and receive, buffered channels and blocking, close, range and select, timeouts, channel directions, worker pools and done signals.
- Synchronization — Coordinate goroutines safely: why synchronization is needed, sync.WaitGroup, sync.Mutex and RWMutex, sync/atomic, and how to detect and prevent race conditions.
Advanced Go
- Reflection — Go reflection with the reflect package: reflect.Type, reflect.Value and Kind, inspecting types and struct fields and tags, reading and setting values, and DeepEqual.
- Context — Go's context package: context.Context and Background, cancellation, deadlines and timeouts, context values, and using context in HTTP requests, goroutines and database calls.
Reference
- Go Cheat Sheet — A lookup page for the whole course: one table per topic, from data types and keywords to files, JSON, goroutines and context.