The Golang Handbook
A Complete Guide to Building Production-Grade Applications in Golang
Introduction: Why Go and Why Now?
- What Makes Go Different
- Who This Book Is For
- How to Use This Book
- What You Will Need
- A Note on Go Versions
Chapter 1: Welcome to Go - History, Philosophy and First Steps
- The Birth of Go at Google
- Design Philosophy: Simplicity, Readability and Pragmatism
- Installing Go and Setting Up Your Environment
- Your First Go Program: Anatomy of
main.go - The Go Toolchain: Build, Run and Format
- Your First Project: A Temperature Converter
- Summary and Key Takeaways
Chapter 2: Language Fundamentals - Variables, Types and Control Flow
- Variables, Constants and Zero Values
- Primitive Types: Integers, Floats, Booleans and Strings
- Type Inference with
varand Short Declaration:= - Control Structures: if/else, switch and for Loops
- Operators and Precedence
- Summary and Key Takeaways
Chapter 3: Functions, Packages and Modules
- Defining and Calling Functions
- Multiple Return Values and Named Returns
- Variadic Functions and
...Syntax - Package Declaration, Imports and Visibility Rules
- Go Modules:
go.mod, Dependency Management and Versioning - The
init()Function - Summary and Key Takeaways
Chapter 4: Pointers, Structs and Methods
- Understanding Pointers:
&and*Operators - When to Use Pointers vs. Values
- Defining and Using Structs
- Struct Tags and Their Purpose
- Methods: Value Receivers vs. Pointer Receivers
- Summary and Key Takeaways
Chapter 5: Interfaces and Duck Typing in Go
- What Is an Interface and Why Go Does It Differently
- Implicit Implementation: No
implementsKeyword - The Empty Interface
interface{}and Type Assertions - Designing Small Interfaces:
io.Reader,io.Writerand the Standard Library Pattern - Interface Internals: How Interfaces Work Under the Hood
- Summary and Key Takeaways
Chapter 6: Error Handling, Defer, Panic and Recover
- Errors as Values: The
errorInterface - Creating Custom Error Types with
errors.Newandfmt.Errorf - Wrapped Errors and
errors.Is/errors.As(Go 1.13+) defer: Deferred Function Execution and Stack Unwindingpanicandrecover: When to Use Them (and When Not To)- Error Handling: The Verbosity Debate
- Summary and Key Takeaways
Chapter 7: Slices, Maps and Collections
- Arrays: Fixed-Length Sequences
- Slices: Dynamic Views onto Arrays
- Slice Internals: Length, Capacity and
make() - The
appendFunction and Slice Aliasing Pitfalls - Maps: Hash Tables in Go
- Summary and Key Takeaways
Chapter 8: Generics in Go - Type Parameters and Constraints
- Why Generics Took So Long: The Design Debate
- Type Parameters and Generic Functions
- Type Constraints: Interfaces as Constraints and
comparable - Generic Structs and Methods
- Practical Patterns: Collections, Algorithms and API Libraries
- Summary and Key Takeaways
Chapter 9: File I/O and Disk Operations
- Reading and Writing Files:
os.ReadFileandos.WriteFile - Opening Files:
os.Open,os.Createandos.OpenFile - Buffered I/O with
bufio - Reading Files Line by Line with
bufio.Scanner - Writing Files with
bufio.Writer - Atomic File Writes: Preventing Partial Reads
- Path Manipulation with
path/filepath - Walking Directories with
filepath.Walkandfilepath.WalkDir - File Metadata and Permissions with
os.Stat - Streaming Large Files: Chunked Reading
- Logging to Disk: A Practical Example
- Temporary Files and Directories
- Summary and Key Takeaways
Chapter 10: Strings, Text Processing and Encoding
- Strings in Go: Immutable Byte Sequences
- UTF-8 and Rune Handling
- String Operations:
strings,strconvandfmtPackages - Regular Expressions with
regexp - JSON and XML Encoding/Decoding
- CSV and Other Data Formats
- Summary and Key Takeaways
Chapter 11: Concurrency - Goroutines, Channels and Synchronization
- Goroutines: Lightweight Concurrent Execution
- Channels: Communication Between Goroutines
- Buffered vs. Unbuffered Channels
select: Multiplexing Channel Operations- The
syncPackage: Mutexes, WaitGroups, Once and More - Concurrency Patterns: Worker Pools, Fan-In/Fan-Out, Pipelines
- Advanced Concurrency Debugging: The Race Detector
- Execution Tracing for Concurrency Analysis
- Common Concurrency Failure Modes
- Channels vs. Mutexes: A Balanced Perspective
- Summary and Key Takeaways
Chapter 12: Context, Cancellation and Request Scopes
- What Is Context and Why It Matters
- Creating Contexts:
background,todoand Derived Contexts - Timeouts and Deadlines with
context.WithTimeoutandWithDeadline - Cancellation Propagation in Concurrent Programs
- Storing Request-Scoped Values (and When Not To)
- Production Case Study: Context Cancellation Across Microservices
- Context Best Practices and Common Mistakes
- Summary and Key Takeaways
Chapter 13: Testing, Benchmarking and Profiling
- The
testingPackage: Writing Unit Tests - Table-Driven Tests: Go’s Idiomatic Testing Pattern
- Test Helpers, Subtests and Parallel Tests
- Benchmarking with
go test -bench - Fuzzing with
go test -fuzz(Go 1.18+) - Profiling: CPU, Memory and Block Profiles with
pprof - Profiling-Driven Optimization: A Systematic Workflow
- Integration Testing and Test Fixtures
- Summary and Key Takeaways
Chapter 14: Web Development and HTTP APIs
- Building HTTP Servers with
net/http - HTTP Clients and Making Requests
- Building RESTful APIs: Routing, Middleware and Handlers
- JSON Request/Response Handling
- Summary and Key Takeaways
Chapter 15: Data Persistence and Distributed Communication
- Database Access:
database/sql, Drivers and Connection Pools - ORM vs. Raw SQL: A Balanced Perspective
- gRPC: Protobuf Definitions and Server/Client Implementation
- WebSockets with
gorilla/websocketornhooyr.io/websocket - Summary and Key Takeaways
Chapter 16: Production Go – Architecture, Deployment and Beyond
- Project Organization: Standard Go Project Layout
- Clean Architecture and Dependency Injection in Go
- Logging:
log/slogand Structured Logging - Configuration Management: Environment Variables, Viper and Flags
- Security Best Practices: Authentication, Input Validation and Secrets
- CLI Applications with
cobraandviper - Containerization with Docker and Multi-Stage Builds
- CI/CD Pipelines for Go Applications
- Microservice Patterns: Service Discovery, Circuit Breakers and Message Queues
- Performance Optimization: Escape Analysis, Allocation Reduction and GC Tuning
- Memory Management and Garbage Collection Internals
- Reflection:
reflectPackage and When to Use It - Summary and Key Takeaways
Conclusion: Your Journey as a Go Engineer
- What You Have Learned
- The Go Mindset
- What Comes Next
- Final Thoughts
