Overview

1 Getting started

This opening chapter sets the tone and goals of the book: learn Go by building realistic projects while developing an instinct for idiomatic, readable, and testable code. It argues that although Go is easy to pick up, mastering it requires understanding the language’s conventions and mechanics. Aimed at experienced developers, it champions a hands-on, example-driven path—building command-line tools, concurrent programs, and HTTP services—so readers can see when and why to use specific features, avoid common pitfalls, and cultivate a pragmatic mindset focused on maintainability.

At a high level, the chapter surveys Go’s core strengths. Go is statically and strongly typed yet ergonomic, compiles fast to efficient native binaries, and ships a runtime that provides a lightweight scheduler and concurrent garbage collector. Concurrency is first-class via goroutines and channels inspired by CSP, enabling scalable, understandable pipelines—used judiciously rather than by default. Go’s type system favors composition over inheritance and achieves flexible, reliable polymorphism through implicit interfaces, which in turn promotes modular design and inherently testable code (for example, swapping real dependencies with fakes in tests by depending on small interfaces).

The chapter also highlights Go’s rich standard library and cohesive tooling. Developers can quickly assemble robust servers and clients with batteries-included packages, while the go tool offers unified commands for building, testing, formatting, vetting, documenting, and managing modules—driving consistency and speed. It explains why Go was created to remove unnecessary complexity and where it excels (CLIs, services, distributed systems), notes its widespread adoption, and closes with practical next steps: install Go, initialize a module, and proceed through the book and appendices to deepen fluency in idioms, testing, and real-world program structure.

The Go compiler compiles code with all necessary dependencies into a single executable binary targeted for a specific operating system and architecture.
A statically linked binary that includes everything it needs to run.
The go statement before a function spawns a new goroutine that runs the given function concurrently. This visual illustrates running the crawl function concurrently in two separate goroutines.
The scheduler multiplexes goroutines onto threads for efficiency. Parallelism occurs at runtime when multiple CPU cores can run multiple threads in parallel.
Goroutines that are running concurrently communicate over channels. These goroutines forward the message to the next goroutine, each performing some computation on the received message before sending it to the next one.
While inheritance creates a type hierarchy, embedding does not. In inheritance, you can pass parent (square) and child (circle) types interchangeably to a function that expects only the parent type. With embedding, the function can only work with the specified type. Embedding does not form a type hierarchy. Embedding simply makes one type's methods and data part of another type.
The function expects a type that implements the runner interface (any type with a run() method). You can interchangeably pass either of these types to the function because each has a run() method and implicitly satisfies runner.
This expensive test runs code that invokes the Charge method of Mithril's API and contacts live servers. It's not the best way of testing.
Testing the code with a fake payment provider. The code no longer uses Mithril's API methods. Instead, it uses a fake payment provider.

Summary

  • This book will be your guide to writing idiomatic and testable code.
  • Learning through examples is an effective way to learn a new technology.
  • Go is a simple and effective programming language.
  • Go fuses the best sides of dynamically and statically typed languages.
  • Go code is written in packages and compiled into efficient native machine code.
  • The compiler produces a single static binary, making deployment straightforward.
  • Go executables include the Go Runtime, which has a scheduler for concurrent execution and a garbage collector that reclaims unreferenced memory.
  • Concurrency is built into Go. There is no difference between writing sequential and concurrent code. It is straightforward to turn the former into the latter.
  • The Go scheduler abstracts the operating system scheduler and threads.
  • Goroutines are independently running concurrent functions.
  • Channels allow goroutines to communicate and synchronize with each other.
  • Go's type system is flat and favors composition over inheritance.
  • Interfaces provide polymorphism and are implicitly satisfied.
  • The standard library includes a comprehensive set of packages that support a broad range of programming tasks, facilitating rapid development.
  • Go's extensive tooling streamlines development workflows.

FAQ

Who is this book for and what are its main goals?It’s written for experienced programmers (including those new to Go) and uses practical, realistic examples. The three goals are: learn by example, write idiomatic and readable Go, and craft testable and maintainable code.
What does “idiomatic Go” mean, and why does it matter?Idiomatic Go is the community’s shared set of conventions aligned with Go’s design principles. Idiomatic code is simple, pragmatic, explicit, and testable—making programs easier to read, maintain, and evolve.
Why is testable code emphasized, and is this a TDD book?Automated tests help ensure code continues to work as it changes, but 100% coverage doesn’t guarantee bug-free software. The book focuses on writing inherently testable code; it doesn’t mandate TDD—tests may come before, during, or after coding depending on the chapter’s goals.
Why choose Go?Go was designed to balance simplicity, reliability, and efficiency: fast compilation, a lean feature set, great performance, and strong tooling. Its stability promise (Go 1 compatibility) and real-world adoption make it a dependable choice for modern software.
How does Go compile and distribute programs?Go compiles directly to native machine code and produces static binaries that include the Go runtime (scheduler, garbage collector, and low-level services). There’s no VM or interpreter required; you compile per target OS/architecture and deploy a single executable.
What are goroutines, and how do they differ from threads?Goroutines are lightweight, concurrent functions started with the “go” keyword. They have tiny, growing stacks and are multiplexed onto OS threads by the Go scheduler, letting you run massive concurrency without heavyweight thread management.
What are channels, and when should I use them vs. mutexes?Channels are safe communication links between goroutines that simplify synchronization and data flow (e.g., pipeline patterns). Prefer channels for passing data and coordinating work; use mutexes when directly protecting shared state is simpler.
How does Go replace inheritance?Go favors composition via struct embedding instead of class-based inheritance. Embedding shares behavior without creating type hierarchies; types remain distinct and aren’t interchangeable purely due to embedding—use interfaces for polymorphism.
What are implicit interfaces, and why are they powerful?Interfaces in Go are satisfied implicitly: any type that has the required methods implements the interface. This enables flexible, decoupled designs with compile-time safety and supports evolving APIs without heavy refactoring.
How do implicit interfaces improve testing, especially with third-party APIs?Define small interfaces around the behavior you need (e.g., a Charger with a Charge method) and pass them into your code. In tests, supply a fake implementation; in production, pass the real client—no need for the third party to expose its own interface.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Go by Example ebook for free
choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Go by Example ebook for free
choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Go by Example ebook for free