Book description
Go is rapidly becoming the preferred language for building web services. While there are plenty of tutorials available that teach Go's syntax to developers with experience in other programming languages, 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. You'll also get a preview of Go's upcoming generics support and how it fits into the language.
- Learn how to write idiomatic code in Go and design a Go project
- Understand the reasons for the design decisions in Go
- 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
Publisher resources
Table of contents
- Preface
- 1. Setting Up Your Go Environment
- 2. Primitive Types and Declarations
- 3. Composite Types
- 4. Blocks, Shadows, and Control Structures
- 5. Functions
- 6. Pointers
-
7. Types, Methods, and Interfaces
- Types in Go
- Methods
- 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
- 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)
- Wrapping Up
- 8. Errors
- 9. Modules, Packages, and Imports
-
10. 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
- The Done Channel Pattern
- Using a Cancel Function to Terminate a Goroutine
- When to Use Buffered and Unbuffered Channels
- Backpressure
- Turning Off a case in a select
- How to Time Out Code
- Using WaitGroups
- Running Code Exactly Once
- Putting Our Concurrent Tools Together
- When to Use Mutexes Instead of Channels
- AtomicsâYou Probably Donât Need These
- Where to Learn More About Concurrency
- Wrapping Up
- 11. The Standard Library
- 12. The Context
- 13. Writing Tests
-
14. Here There Be Dragons: Reflect,
Unsafe, and Cgo
-
Reflection Lets Us Work with Types at Runtime
- Types, Kinds, and Values
- Making 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
- Only Use Reflection If Itâs Worthwhile
- unsafe Is Unsafe
- Cgo Is for Integration, Not Performance
- Wrapping Up
-
Reflection Lets Us Work with Types at Runtime
-
15. Welcome to the Future: Generics in Go
- 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
- Things That Are Left Out
- Idiomatic Go and Generics
- Further Futures Unlocked
- Wrapping Up
- Index
- About the Author
Product information
- Title: Learning Go
- Author(s):
- Release date: March 2021
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781492077169
You might also like
book
Learning Go, 2nd Edition
Go has rapidly become the preferred language for building web services. Plenty of tutorials are available …
book
Head First Go
What will you learn from this book? Go makes it easy to build software that’s simple, …
book
Learning Helm
Get up to speed with Helm, the preeminent package manager for the Kubernetes container orchestration system. …
book
Istio in Action
Solve difficult service-to-service communication challenges around security, observability, routing, and resilience with an Istio-based service mesh. …