Overview

Want to quickly master Go through working examples? This by-example guide teaches 95% of Go through 85+ annotated code examples organized by complexity level.

What Is By-Example Learning?

By-example learning is an example-first approach where you learn through annotated, runnable code rather than narrative explanations. Each example is self-contained, immediately executable with go run, and heavily commented to show:

  • What each line does - Inline comments explain the purpose and mechanism
  • Expected outputs - Using // => notation to show results
  • Intermediate values - Variable states and control flow made visible
  • Key takeaways - 1-2 sentence summaries of core concepts

This approach is ideal for experienced developers (seasonal programmers or software engineers) who are familiar with at least one programming language and want to quickly understand Go’s syntax, idioms, and unique features through working code.

Unlike narrative tutorials that build understanding through explanation and storytelling, by-example learning lets you see the code first, run it second, and understand it through direct interaction. You learn by doing, not by reading about doing.

Learning Path

The Go by-example tutorial guides you through 75-90 examples organized into three progressive levels, from fundamental concepts to advanced patterns.

  %% Color Palette: Blue #0173B2, Orange #DE8F05, Teal #029E73, Purple #CC78BC, Brown #CA9161
graph TD
    A["Beginner (Examples 1-30)<br/>0-40% Coverage<br/>Fundamentals"]
    B["Intermediate (Examples 31-60)<br/>40-75% Coverage<br/>Production Ready"]
    C["Advanced (Examples 61-90)<br/>75-95% Coverage<br/>Expert Mastery"]

    A -->|Master foundations| B
    B -->|Advanced patterns| C

    style A fill:#0173B2,stroke:#000,color:#fff
    style B fill:#DE8F05,stroke:#000,color:#fff
    style C fill:#029E73,stroke:#000,color:#fff

Coverage Philosophy

This by-example guide provides 95% coverage of Go through practical, annotated examples. The 95% figure represents the depth and breadth of concepts covered, not a time estimate—focus is on outcomes and understanding, not duration.

What’s Covered

  • Core syntax - Variables, types, operators, control flow, methods
  • Data structures - Arrays, slices, maps, structs, and their underlying mechanics (backing arrays, capacity, zero values)
  • Functions and methods - Function declarations, multiple returns, methods, receivers (value vs pointer), and when to use each
  • Interfaces and composition - Interface definition, implicit satisfaction, type assertions, and Go’s composition-over-inheritance philosophy
  • Error handling - The error interface, custom error types, error wrapping, and the error handling idiom
  • Concurrency fundamentals - Goroutines, channels (buffered and unbuffered), the select statement, synchronization primitives (sync.WaitGroup, sync.Mutex)
  • Standard library - Deep dive into fmt, strings, encoding/json, net/http, time, regexp, context, and other essential packages
  • I/O and HTTP - File operations, HTTP clients and servers, handler functions, middleware patterns
  • Production patterns - HTTP middleware chains (request/response decoration), graceful shutdown with signal handling, worker pools, options pattern for configuration
  • Testing and benchmarking - Table-driven tests, subtests, benchmarking, fuzzing, test coverage measurement
  • Advanced concurrency - Pipeline patterns, fan-out/fan-in, rate limiting, semaphores, atomic operations
  • Modern Go features - Generics (Go 1.18+), embed directive (Go 1.16+), fuzzing (Go 1.18+), workspaces (Go 1.18+)
  • Advanced tools - Reflection, CGO, build tags, custom sorting, dependency injection, memory profiling, race detector

What’s NOT Covered

This guide focuses on learning-oriented examples, not problem-solving recipes or production deployment. For additional topics:

  • Framework-specific content - Gin, Echo, Chi (standard library foundation taught here applies to all frameworks)
  • Database drivers - SQL, MongoDB packages (driver APIs vary, learn standard library patterns instead)
  • Deployment and DevOps - Docker, Kubernetes, cloud deployment (infrastructure beyond the language)
  • Go internals - Scheduler algorithms, garbage collector internals, memory layout
  • Ecosystem tools - Profiling tools beyond pprof, code generation tools, specialized libraries

The 95% coverage goal maintains humility—no tutorial can cover everything. This guide teaches the core concepts that unlock the remaining 5% through your own exploration and project work.

How to Use This Guide

  1. Sequential or selective - Read examples in order for progressive learning, or jump to specific topics when switching from another language
  2. Run everything - Copy and paste examples into a file and execute with go run. Experimentation solidifies understanding.
  3. Modify and explore - Change values, add print statements, break things intentionally. Learn through experimentation.
  4. Use as reference - Bookmark examples for quick lookups when you forget syntax or patterns
  5. Complement with narrative tutorials - By-example learning is code-first; pair with comprehensive tutorials for deeper explanations

Best workflow: Open your editor or terminal in one window, this guide in another. Run each example as you read it. When you encounter something unfamiliar, run the example, modify it, see what changes.

Reference System: Examples are numbered (1-90) and grouped by level. This numbering appears in other Go content at ayokoding.com, allowing you to reference specific examples elsewhere.

Structure of Each Example

Every example follows a consistent five-part format:

  1. Brief Explanation (2-3 sentences): What the example demonstrates and why it matters
  2. Mermaid Diagram (optional): Visual clarification when concept relationships benefit from visualization
  3. Heavily Annotated Code: Every significant line includes a comment explaining what it does and what it produces (using // => notation)
  4. Key Takeaway (1-2 sentences): The core insight you should retain from this example

This structure minimizes context switching - explanation, visual aid, runnable code, and distilled essence all in one place.

Relationship to Other Tutorials

This by-example tutorial complements other learning approaches. Choose based on your situation:

Tutorial TypeCoverageBest ForLearning Style
Quick Start5-30%Getting something working quicklyHands-on with guided structure
Beginner0-60%Learning from scratchNarrative explanations with examples
This: By Example95%Rapid depth for experienced devsCode-first, minimal explanation
CookbookParallelSolving specific problemsProblem-solution recipes
Advanced85-95%Expert masteryDeep dives and edge cases

By-example is ideal if you have programming experience in other languages. It accelerates learning by leveraging your existing knowledge - you focus on “how Go does this” rather than learning programming concepts from scratch.

The 95% coverage represents depth and breadth of topics you’ll encounter in production Go code. It explicitly acknowledges that no tutorial covers everything, but these examples provide the foundation to understand the remaining 5% through official documentation, source code, and community resources.

Prerequisites

  • Basic programming experience in any language (Python, JavaScript, Java, C++, etc.)
  • Go installed and verified (follow the initial-setup tutorial if needed)
  • A code editor you’re comfortable with (VS Code with Go extension, GoLand, Vim, etc.)

You don’t need to understand Go’s internals, philosophy, or ecosystem yet - this tutorial teaches those through examples. You just need comfort reading and running code.

Learning Strategies

For Python/JavaScript Developers

You’re used to dynamic typing and flexible syntax. Go will feel more structured but simpler than Java:

  • Static typing with inference: Types are checked at compile time, but var and := reduce verbosity
  • No classes: Use structs and methods instead of OOP inheritance
  • Explicit error handling: No exceptions, check returned error values

Focus on Examples 5-12 (types and structs) and Examples 20-25 (error handling) to build Go intuition.

For Java/C# Developers

You understand static typing and OOP. Go simplifies and changes the paradigm:

  • Composition over inheritance: Embed structs instead of extending classes
  • Interfaces are implicit: No implements keyword, just satisfy the method set
  • No generics complexity: Generics added in Go 1.18, simpler than Java’s

Focus on Examples 15-20 (interfaces) and Examples 31-40 (goroutines and channels) to leverage your OOP knowledge.

For C/C++ Developers

You understand pointers and systems programming. Go modernizes the experience:

  • Garbage collection: No manual memory management, but pointers exist
  • Goroutines vs threads: Lightweight concurrency without thread management
  • Built-in tooling: go fmt, go test, go build standardize workflow

Focus on Examples 55-65 (unsafe code, CGO) and Examples 70-80 (performance profiling) to apply systems knowledge.

For Rust Developers

You know ownership and systems programming. Go trades safety for simplicity:

  • GC instead of ownership: No borrow checker, runtime handles memory
  • Simpler concurrency: Goroutines and channels vs async/await
  • Faster compilation: Near-instant builds for rapid iteration

Focus on Examples 31-50 (concurrency patterns) to see Go’s approach to safe concurrent programming.

Code-First Philosophy

This tutorial prioritizes working code over theoretical discussion:

  • No lengthy prose: Concepts are demonstrated, not explained at length
  • Runnable examples: Every example compiles and runs with go run
  • Learn by doing: Understanding comes from running and modifying code
  • Pattern recognition: See the same patterns in different contexts across 85 examples

If you prefer narrative explanations, consider the by-concept tutorial (available separately). By-example learning works best when you learn through experimentation.

Comparison with By-Example for Other Languages

Other languages at ayokoding.com have similar by-example tutorials:

  • Java By-Example: 75 examples covering OOP, streams, concurrency, JVM patterns
  • Elixir By-Example: 90 examples covering functional programming, pattern matching, concurrency, OTP

The Go version follows the same philosophy and structure but emphasizes Go-specific strengths: simplicity, concurrency, static compilation, and the extensive standard library.

Last updated