Go Fundamentals
Core Go language features, error handling, interfaces, and concurrency.
Key Concepts
+ Add ConceptGo Context Cancellation
Go Context Cancellation
context.Context carries cancellation signals, deadlines, and request-scoped values across API boundaries. Derived contexts must have their cancellation functions called when no longer needed.
Go Defer Panic and Recover
Go Defer Panic and Recover
defer schedules a call for function exit, panic starts stack unwinding, and recover can stop a panic only when called directly from a deferred function in the same goroutine.
Go Error Handling
Go Error Handling
Go represents expected failures as explicit error return values. Callers check errors close to the operation, wrap them with context using %w, and inspect chains with errors.Is or errors.As.
Go Generics and Constraints
Go Generics and Constraints
Go generics use type parameters with constraints to write reusable algorithms while retaining compile-time type checking. Constraints describe the operations or underlying types that a type argument must support.
Go Goroutines and Channels
Go Goroutines and Channels
Goroutines are lightweight concurrent functions managed by the Go runtime. Channels let goroutines synchronize and exchange typed values, while cancellation and ownership prevent leaks and deadlocks.
Go Interfaces and Nil
Go Interfaces and Nil
Go types implement interfaces implicitly by providing the required method set. An interface value contains a dynamic type and dynamic value, so an interface holding a typed nil pointer is not itself nil.
Go Maps
Go Maps
Go maps associate comparable keys with values. A lookup of an absent key returns the value type’s zero value, so the comma-ok form distinguishes absence from an explicitly stored zero value. Ordinary maps are not safe for concurrent reads and writes.
Go Method Receivers
Go Method Receivers
Value receiver methods receive a copy of the value, while pointer receiver methods receive a pointer and can mutate the original. Receiver choice also affects interface satisfaction and whether calling a method copies synchronization primitives or large structs.
Go Slices and Backing Arrays
Go Slices and Backing Arrays
A slice is a small descriptor containing a pointer to an underlying array plus length and capacity. Copying a slice copies that descriptor, so slices can share storage until an append allocates a new backing array.
Go Struct Embedding
Go Struct Embedding
Embedding a type as an unnamed field promotes its accessible fields and methods to the outer type. It is composition rather than inheritance: the embedded value remains a distinct component and name conflicts require explicit selection.
Go Synchronization and Data Races
Go Synchronization and Data Races
A data race occurs when concurrent goroutines access the same memory, at least one access is a write, and the accesses are not synchronized. Go provides mutexes, atomics, channels, and the race detector to make ownership and synchronization explicit.
Go Table-Driven Testing
Go Table-Driven Testing
Table-driven tests express multiple inputs and expected outputs as data, then run a shared assertion body for each case. Subtests give each case a clear name and make failures easy to isolate.