Book description
Spot errors in your Go code you didn’t even know you were making and boost your productivity by avoiding common mistakes and pitfalls.100 Go Mistakes and How to Avoid Them shows you how to:
- Dodge the most common mistakes made by Go developers
- Structure and organize your Go application
- Handle data and control structures efficiently
- Deal with errors in an idiomatic manner
- Improve your concurrency skills
- Optimize your code
- Make your application production-ready and improve testing quality
100 Go Mistakes and How to Avoid Them puts a spotlight on common errors in Go code you might not even know you’re making. You’ll explore key areas of the language such as concurrency, testing, data structures, and more—and learn how to avoid and fix mistakes in your own projects. As you go, you’ll navigate the tricky bits of handling JSON data and HTTP services, discover best practices for Go code organization, and learn how to use slices efficiently.
About the Technology
Understanding mistakes is the best way to improve the quality of your code. This unique book examines 100 bugs and inefficiencies common to Go applications, along with tips and techniques to avoid making them in your own projects.
About the Book
100 Go Mistakes and How to Avoid Them shows you how to replace common programming problems in Go with idiomatic, expressive code. In it, you’ll explore dozens of interesting examples and case studies as you learn to spot mistakes that might appear in your own applications. Expert author Teiva Harsanyi organizes the error avoidance techniques into convenient categories, ranging from types and strings to concurrency and testing.
What's Inside
- Identify and squash code-level bugs
- Avoid problems with application structure and design
- Perfect your data and control structures
- Optimize your code by eliminating inefficiencies
About the Reader
For developers proficient with Go programming and syntax.
About the Author
Teiva Harsanyi is a senior software engineer at Docker with experience in various domains, including safety-critical industries like air traffic management.
Quotes
Required reading for Go developers before they touch code in production. It’s the Go equivalent of Effective Java.
- Neeraj Shah, Nutanix
Clear and effective examples. Armed with the understanding of how and why mistakes occur, you’re equipped to avoid costly errors.
- Giuseppe Maxia, Vmware
Teiva Harsanyi catalogs real problems and easy-to-miss ‘gotchas,’ and delves into the nuanced world of why they happen. Not having this book will be your 101st mistake.
- Anupam Sengupta, Red Hat
Learn good habits by identifying bad ones. The writing is engaging, the examples relevant, and the insights useful.
- Thad Meyer, LI-COR Biosciences
Publisher resources
Table of contents
- inside front cover
- 100 Go Mistakes
- Copyright
- dedication
- contents
- front matter
- 1 Go: Simple to learn but hard to master
-
2 Code and project organization
- 2.1 #1: Unintended variable shadowing
- 2.2 #2: Unnecessary nested code
- 2.3 #3: Misusing init functions
- 2.4 #4: Overusing getters and setters
- 2.5 #5: Interface pollution
- 2.6 #6: Interface on the producer side
- 2.7 #7: Returning interfaces
- 2.8 #8: any says nothing
- 2.9 #9: Being confused about when to use generics
- 2.10 #10: Not being aware of the possible problems with type embedding
- 2.11 #11: Not using the functional options pattern
- 2.12 #12: Project misorganization
- 2.13 #13: Creating utility packages
- 2.14 #14: Ignoring package name collisions
- 2.15 #15: Missing code documentation
- 2.16 #16: Not using linters
- Summary
-
3 Data types
- 3.1 #17: Creating confusion with octal literals
- 3.2 #18: Neglecting integer overflows
- 3.3 #19: Not understanding floating points
- 3.4 #20: Not understanding slice length and capacity
- 3.5 #21: Inefficient slice initialization
- 3.6 #22: Being confused about nil vs. empty slices
- 3.7 #23: Not properly checking if a slice is empty
- 3.8 #24: Not making slice copies correctly
- 3.9 #25: Unexpected side effects using slice append
- 3.10 #26: Slices and memory leaks
- 3.11 #27: Inefficient map initialization
- 3.12 #28: Maps and memory leaks
- 3.13 #29: Comparing values incorrectly
- Summary
-
4 Control structures
- 4.1 #30: Ignoring the fact that elements are copied in range loops
- 4.2 #31: Ignoring how arguments are evaluated in range loops
- 4.3 #32: Ignoring the impact of using pointer elements in range loops
- 4.4 #33: Making wrong assumptions during map iterations
- 4.5 #34: Ignoring how the break statement works
- 4.6 #35: Using defer inside a loop
- Summary
- 5 Strings
-
6 Functions and methods
- 6.1 #42: Not knowing which type of receiver to use
- 6.2 #43: Never using named result parameters
- 6.3 #44: Unintended side effects with named result parameters
- 6.4 #45: Returning a nil receiver
- 6.5 #46: Using a filename as a function input
- 6.6 #47: Ignoring how defer arguments and receivers are evaluated
- Summary
- 7 Error management
-
8 Concurrency: Foundations
- 8.1 #55: Mixing up concurrency and parallelism
- 8.2 #56: Thinking concurrency is always faster
- 8.3 #57: Being puzzled about when to use channels or mutexes
- 8.4 #58: Not understanding race problems
- 8.5 #59: Not understanding the concurrency impacts of a workload type
- 8.6 #60: Misunderstanding Go contexts
- Summary
-
9 Concurrency: Practice
- 9.1 #61: Propagating an inappropriate context
- 9.2 #62: Starting a goroutine without knowing when to stop it
- 9.3 #63: Not being careful with goroutines and loop variables
- 9.4 #64: Expecting deterministic behavior using select and channels
- 9.5 #65: Not using notification channels
- 9.6 #66: Not using nil channels
- 9.7 #67: Being puzzled about channel size
- 9.8 #68: Forgetting about possible side effects with string formatting
- 9.9 #69: Creating data races with append
- 9.10 #70: Using mutexes inaccurately with slices and maps
- 9.11 #71: Misusing sync.WaitGroup
- 9.12 #72: Forgetting about sync.Cond
- 9.13 #73: Not using errgroup
- 9.14 #74: Copying a sync type
- Summary
-
10 The standard library
- 10.1 #75: Providing a wrong time duration
- 10.2 #76: time.After and memory leaks
- 10.3 #77: Common JSON-handling mistakes
- 10.4 #78: Common SQL mistakes
- 10.5 #79: Not closing transient resources
- 10.6 #80: Forgetting the return statement after replying to an HTTP request
- 10.7 #81: Using the default HTTP client and server
- Summary
-
11 Testing
- 11.1 #82: Not categorizing tests
- 11.2 #83: Not enabling the -race flag
- 11.3 #84: Not using test execution modes
- 11.4 #85: Not using table-driven tests
- 11.5 #86: Sleeping in unit tests
- 11.6 #87: Not dealing with the time API efficiently
- 11.7 #88: Not using testing utility packages
- 11.8 #89: Writing inaccurate benchmarks
- 11.9 #90: Not exploring all the Go testing features
- Summary
-
12 Optimizations
- 12.1 #91: Not understanding CPU caches
- 12.2 #92: Writing concurrent code that leads to false sharing
- 12.3 #93: Not taking into account instruction-level parallelism
- 12.4 #94: Not being aware of data alignment
- 12.5 #95: Not understanding stack vs. heap
- 12.6 #96: Not knowing how to reduce allocations
- 12.7 #97: Not relying on inlining
- 12.8 #98: Not using Go diagnostics tooling
- 12.9 #99: Not understanding how the GC works
- 12.10 #100: Not understanding the impacts of running Go in Docker and Kubernetes
- Summary
- Final words
- index
Product information
- Title: 100 Go Mistakes and How to Avoid Them
- Author(s):
- Release date: September 2022
- Publisher(s): Manning Publications
- ISBN: 9781617299599
You might also like
video
100 Go Mistakes and How to Avoid Them, Video Edition
In Video Editions the narrator reads the book while the content, figures, code listings, diagrams, and …
book
Head First Go
What will you learn from this book? Go makes it easy to build software that’s simple, …
book
Tidy First?
Messy code is a nuisance. "Tidying" code, to make it more readable, requires breaking it up …
book
Microservices Patterns
Microservices Patterns teaches enterprise developers and architects how to build applications with the microservice architecture. Rather …