Book description
Go has rapidly become the preferred language for building web services. Plenty of tutorials are available to teach Go's syntax to developers with experience in other programming languages, but tutorials aren't enough. They don't teach Go's idioms, so developers end up recreating patterns that don't make sense in a Go context. This practical guide provides the essential background you need to write clear and idiomatic Go.
No matter your level of experience, you'll learn how to think like a Go developer. Author Jon Bodner introduces the design patterns experienced Go developers have adopted and explores the rationale for using them. This updated edition also shows you how Go's generics support fits into the language.
This book helps you:
- Write idiomatic code in Go and design a Go project
- Understand the reasons behind Go's design decisions
- Set up a Go development environment for a solo developer or team
- Learn how and when to use reflection, unsafe, and cgo
- Discover how Go's features allow the language to run efficiently
- Know which Go features you should use sparingly or not at all
- Use Go's tools to improve performance, optimize memory usage, and reduce garbage collection
- Learn how to use Go's advanced development tools
Publisher resources
Table of contents
- Preface
- 1. Setting Up Your Go Environment
- 2. Predeclared Types and Declarations
- 3. Composite Types
- 4. Blocks, Shadows, and Control Structures
- 5. Functions
-
6. Pointers
- A Quick Pointer Primer
- Don’t Fear the Pointers
- Pointers Indicate Mutable Parameters
- Pointers Are a Last Resort
- Pointer Passing Performance
- The Zero Value Versus No Value
- The Difference Between Maps and Slices
- Slices as Buffers
- Reducing the Garbage Collector’s Workload
- Tuning the Garbage Collector
- Exercises
- Wrapping Up
-
7. Types, Methods, and Interfaces
- Types in Go
- Methods
- iota Is for Enumerations—Sometimes
- Use Embedding for Composition
- Embedding Is Not Inheritance
- A Quick Lesson on Interfaces
- Interfaces Are Type-Safe Duck Typing
- Embedding and Interfaces
- Accept Interfaces, Return Structs
- Interfaces and nil
- Interfaces Are Comparable
- The Empty Interface Says Nothing
- Type Assertions and Type Switches
- Use Type Assertions and Type Switches Sparingly
- Function Types Are a Bridge to Interfaces
- Implicit Interfaces Make Dependency Injection Easier
- Wire
- Go Isn’t Particularly Object-Oriented (and That’s Great)
- Exercises
- Wrapping Up
-
8. Generics
- Generics Reduce Repetitive Code and Increase Type Safety
- Introducing Generics in Go
- Generic Functions Abstract Algorithms
- Generics and Interfaces
- Use Type Terms to Specify Operators
- Type Inference and Generics
- Type Elements Limit Constants
- Combining Generic Functions with Generic Data Structures
- More on comparable
- Things That Are Left Out
- Idiomatic Go and Generics
- Adding Generics to the Standard Library
- Future Features Unlocked
- Exercises
- Wrapping Up
- 9. Errors
-
10. Modules, Packages, and Imports
- Repositories, Modules, and Packages
- Using go.mod
-
Building Packages
- Importing and Exporting
- Creating and Accessing a Package
- Naming Packages
- Overriding a Package’s Name
- Documenting Your Code with Go Doc Comments
- Using the internal Package
- Avoiding Circular Dependencies
- Organizing Your Module
- Gracefully Renaming and Reorganizing Your API
- Avoiding the init Function if Possible
- Working with Modules
- Publishing Your Module
- Versioning Your Module
- Module Proxy Servers
- Additional Details
- Exercises
- Wrapping Up
-
11. Go Tooling
- Using go run to Try Out Small Programs
- Adding Third-Party Tools with go install
- Improving Import Formatting with goimports
- Using Code-Quality Scanners
- Using govulncheck to Scan for Vulnerable Dependencies
- Embedding Content into Your Program
- Embedding Hidden Files
- Using go generate
- Working with go generate and Makefiles
- Reading the Build Info Inside a Go Binary
- Building Go Binaries for Other Platforms
- Using Build Tags
- Testing Versions of Go
- Using go help to Learn More About Go Tooling
- Exercises
- Wrapping Up
-
12. Concurrency in Go
- When to Use Concurrency
- Goroutines
- Channels
- select
-
Concurrency Practices and Patterns
- Keep Your APIs Concurrency-Free
- Goroutines, for Loops, and Varying Variables
- Always Clean Up Your Goroutines
- Use the Context to Terminate Goroutines
- Know When to Use Buffered and Unbuffered Channels
- Implement Backpressure
- Turn Off a case in a select
- Time Out Code
- Use WaitGroups
- Run Code Exactly Once
- Put Your Concurrent Tools Together
- When to Use Mutexes Instead of Channels
- Atomics—You Probably Don’t Need These
- Where to Learn More About Concurrency
- Exercises
- Wrapping Up
- 13. The Standard Library
- 14. The Context
- 15. Writing Tests
-
16. Here Be Dragons: Reflect,
Unsafe, and Cgo
-
Reflection Lets You Work with Types at Runtime
- Types, Kinds, and Values
- Make New Values
- Use Reflection to Check If an Interface’s Value Is nil
- Use Reflection to Write a Data Marshaler
- Build Functions with Reflection to Automate Repetitive Tasks
- You Can Build Structs with Reflection, but Don’t
- Reflection Can’t Make Methods
- Use Reflection Only if It’s Worthwhile
- unsafe Is Unsafe
- Cgo Is for Integration, Not Performance
- Exercises
- Wrapping Up
-
Reflection Lets You Work with Types at Runtime
- Index
- About the Author
Product information
- Title: Learning Go, 2nd Edition
- Author(s):
- Release date: January 2024
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781098139292
You might also like
book
Learning Go
Go is rapidly becoming the preferred language for building web services. While there are plenty of …
book
Head First Software Architecture
What will you learn from this book? If you're a software developer looking for a quick …
book
Head First Design Patterns, 2nd Edition
What will you learn from this book? You know you don't want to reinvent the wheel, …
book
Python Crash Course, 3rd Edition
Python Crash Course is the world's best-selling guide to the Python guide programming language, with over …