100 Go Mistakes and How to Avoid Them

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

View/Submit Errata

Table of contents

  1. inside front cover
  2. 100 Go Mistakes
  3. Copyright
  4. dedication
  5. contents
  6. front matter
    1. preface
    2. acknowledgments
    3. about this book
      1. Who should read this book
      2. How this book is organized: A roadmap
      3. About the code
      4. liveBook discussion forum
    4. about the author
    5. about the cover illustration
  7. 1 Go: Simple to learn but hard to master
    1. 1.1 Go outline
    2. 1.2 Simple doesn’t mean easy
    3. 1.3 100 Go mistakes
      1. 1.3.1 Bugs
      2. 1.3.2 Needless complexity
      3. 1.3.3 Weaker readability
      4. 1.3.4 Suboptimal or unidiomatic organization
      5. 1.3.5 Lack of API convenience
      6. 1.3.6 Under-optimized code
      7. 1.3.7 Lack of productivity
    4. Summary
  8. 2 Code and project organization
    1. 2.1 #1: Unintended variable shadowing
    2. 2.2 #2: Unnecessary nested code
    3. 2.3 #3: Misusing init functions
      1. 2.3.1 Concepts
      2. 2.3.2 When to use init functions
    4. 2.4 #4: Overusing getters and setters
    5. 2.5 #5: Interface pollution
      1. 2.5.1 Concepts
      2. 2.5.2 When to use interfaces
      3. 2.5.3 Interface pollution
    6. 2.6 #6: Interface on the producer side
    7. 2.7 #7: Returning interfaces
    8. 2.8 #8: any says nothing
    9. 2.9 #9: Being confused about when to use generics
      1. 2.9.1 Concepts
      2. 2.9.2 Common uses and misuses
    10. 2.10 #10: Not being aware of the possible problems with type embedding
    11. 2.11 #11: Not using the functional options pattern
      1. 2.11.1 Config struct
      2. 2.11.2 Builder pattern
      3. 2.11.3 Functional options pattern
    12. 2.12 #12: Project misorganization
      1. 2.12.1 Project structure
      2. 2.12.2 Package organization
    13. 2.13 #13: Creating utility packages
    14. 2.14 #14: Ignoring package name collisions
    15. 2.15 #15: Missing code documentation
    16. 2.16 #16: Not using linters
    17. Summary
  9. 3 Data types
    1. 3.1 #17: Creating confusion with octal literals
    2. 3.2 #18: Neglecting integer overflows
      1. 3.2.1 Concepts
      2. 3.2.2 Detecting integer overflow when incrementing
      3. 3.2.3 Detecting integer overflows during addition
      4. 3.2.4 Detecting an integer overflow during multiplication
    3. 3.3 #19: Not understanding floating points
    4. 3.4 #20: Not understanding slice length and capacity
    5. 3.5 #21: Inefficient slice initialization
    6. 3.6 #22: Being confused about nil vs. empty slices
    7. 3.7 #23: Not properly checking if a slice is empty
    8. 3.8 #24: Not making slice copies correctly
    9. 3.9 #25: Unexpected side effects using slice append
    10. 3.10 #26: Slices and memory leaks
      1. 3.10.1 Leaking capacity
      2. 3.10.2 Slice and pointers
    11. 3.11 #27: Inefficient map initialization
      1. 3.11.1 Concepts
      2. 3.11.2 Initialization
    12. 3.12 #28: Maps and memory leaks
    13. 3.13 #29: Comparing values incorrectly
    14. Summary
  10. 4 Control structures
    1. 4.1 #30: Ignoring the fact that elements are copied in range loops
      1. 4.1.1 Concepts
      2. 4.1.2 Value copy
    2. 4.2 #31: Ignoring how arguments are evaluated in range loops
      1. 4.2.1 Channels
      2. 4.2.2 Array
    3. 4.3 #32: Ignoring the impact of using pointer elements in range loops
    4. 4.4 #33: Making wrong assumptions during map iterations
      1. 4.4.1 Ordering
      2. 4.4.2 Map insert during iteration
    5. 4.5 #34: Ignoring how the break statement works
    6. 4.6 #35: Using defer inside a loop
    7. Summary
  11. 5 Strings
    1. 5.1 #36: Not understanding the concept of a rune
    2. 5.2 #37: Inaccurate string iteration
    3. 5.3 #38: Misusing trim functions
    4. 5.4 #39: Under-optimized string concatenation
    5. 5.5 #40: Useless string conversions
    6. 5.6 #41: Substrings and memory leaks
    7. Summary
  12. 6 Functions and methods
    1. 6.1 #42: Not knowing which type of receiver to use
    2. 6.2 #43: Never using named result parameters
    3. 6.3 #44: Unintended side effects with named result parameters
    4. 6.4 #45: Returning a nil receiver
    5. 6.5 #46: Using a filename as a function input
    6. 6.6 #47: Ignoring how defer arguments and receivers are evaluated
      1. 6.6.1 Argument evaluation
      2. 6.6.2 Pointer and value receivers
    7. Summary
  13. 7 Error management
    1. 7.1 #48: Panicking
    2. 7.2 #49: Ignoring when to wrap an error
    3. 7.3 #50: Checking an error type inaccurately
    4. 7.4 #51: Checking an error value inaccurately
    5. 7.5 #52: Handling an error twice
    6. 7.6 #53: Not handling an error
    7. 7.7 #54: Not handling defer errors
    8. Summary
  14. 8 Concurrency: Foundations
    1. 8.1 #55: Mixing up concurrency and parallelism
    2. 8.2 #56: Thinking concurrency is always faster
      1. 8.2.1 Go scheduling
      2. 8.2.2 Parallel merge sort
    3. 8.3 #57: Being puzzled about when to use channels or mutexes
    4. 8.4 #58: Not understanding race problems
      1. 8.4.1 Data races vs. race conditions
      2. 8.4.2 The Go memory model
    5. 8.5 #59: Not understanding the concurrency impacts of a workload type
    6. 8.6 #60: Misunderstanding Go contexts
      1. 8.6.1 Deadline
      2. 8.6.2 Cancellation signals
      3. 8.6.3 Context values
      4. 8.6.4 Catching a context cancellation
    7. Summary
  15. 9 Concurrency: Practice
    1. 9.1 #61: Propagating an inappropriate context
    2. 9.2 #62: Starting a goroutine without knowing when to stop it
    3. 9.3 #63: Not being careful with goroutines and loop variables
    4. 9.4 #64: Expecting deterministic behavior using select and channels
    5. 9.5 #65: Not using notification channels
    6. 9.6 #66: Not using nil channels
    7. 9.7 #67: Being puzzled about channel size
    8. 9.8 #68: Forgetting about possible side effects with string formatting
      1. 9.8.1 etcd data race
      2. 9.8.2 Deadlock
    9. 9.9 #69: Creating data races with append
    10. 9.10 #70: Using mutexes inaccurately with slices and maps
    11. 9.11 #71: Misusing sync.WaitGroup
    12. 9.12 #72: Forgetting about sync.Cond
    13. 9.13 #73: Not using errgroup
    14. 9.14 #74: Copying a sync type
    15. Summary
  16. 10 The standard library
    1. 10.1 #75: Providing a wrong time duration
    2. 10.2 #76: time.After and memory leaks
    3. 10.3 #77: Common JSON-handling mistakes
      1. 10.3.1 Unexpected behavior due to type embedding
      2. 10.3.2 JSON and the monotonic clock
      3. 10.3.3 Map of any
    4. 10.4 #78: Common SQL mistakes
      1. 10.4.1 Forgetting that sql.Open doesn’t necessarily establish connections to a database
      2. 10.4.2 Forgetting about connections pooling
      3. 10.4.3 Not using prepared statements
      4. 10.4.4 Mishandling null values
      5. 10.4.5 Not handling row iteration errors
    5. 10.5 #79: Not closing transient resources
      1. 10.5.1 HTTP body
      2. 10.5.2 sql.Rows
      3. 10.5.3 os.File
    6. 10.6 #80: Forgetting the return statement after replying to an HTTP request
    7. 10.7 #81: Using the default HTTP client and server
      1. 10.7.1 HTTP client
      2. 10.7.2 HTTP server
    8. Summary
  17. 11 Testing
    1. 11.1 #82: Not categorizing tests
      1. 11.1.1 Build tags
      2. 11.1.2 Environment variables
      3. 11.1.3 Short mode
    2. 11.2 #83: Not enabling the -race flag
    3. 11.3 #84: Not using test execution modes
      1. 11.3.1 The parallel flag
      2. 11.3.2 The -shuffle flag
    4. 11.4 #85: Not using table-driven tests
    5. 11.5 #86: Sleeping in unit tests
    6. 11.6 #87: Not dealing with the time API efficiently
    7. 11.7 #88: Not using testing utility packages
      1. 11.7.1 The httptest package
      2. 11.7.2 The iotest package
    8. 11.8 #89: Writing inaccurate benchmarks
      1. 11.8.1 Not resetting or pausing the timer
      2. 11.8.2 Making wrong assumptions about micro-benchmarks
      3. 11.8.3 Not being careful about compiler optimizations
      4. 11.8.4 Being fooled by the observer effect
    9. 11.9 #90: Not exploring all the Go testing features
      1. 11.9.1 Code coverage
      2. 11.9.2 Testing from a different package
      3. 11.9.3 Utility functions
      4. 11.9.4 Setup and teardown
    10. Summary
  18. 12 Optimizations
    1. 12.1 #91: Not understanding CPU caches
      1. 12.1.1 CPU architecture
      2. 12.1.2 Cache line
      3. 12.1.3 Slice of structs vs. struct of slices
      4. 12.1.4 Predictability
      5. 12.1.5 Cache placement policy
    2. 12.2 #92: Writing concurrent code that leads to false sharing
    3. 12.3 #93: Not taking into account instruction-level parallelism
    4. 12.4 #94: Not being aware of data alignment
    5. 12.5 #95: Not understanding stack vs. heap
      1. 12.5.1 Stack vs. heap
      2. 12.5.2 Escape analysis
    6. 12.6 #96: Not knowing how to reduce allocations
      1. 12.6.1 API changes
      2. 12.6.2 Compiler optimizations
      3. 12.6.3 sync.Pool
    7. 12.7 #97: Not relying on inlining
    8. 12.8 #98: Not using Go diagnostics tooling
      1. 12.8.1 Profiling
      2. 12.8.2 Execution tracer
    9. 12.9 #99: Not understanding how the GC works
      1. 12.9.1 Concepts
      2. 12.9.2 Examples
    10. 12.10 #100: Not understanding the impacts of running Go in Docker and Kubernetes
    11. Summary
    12. Final words
  19. index

Product information

  • Title: 100 Go Mistakes and How to Avoid Them
  • Author(s): Teiva Harsanyi
  • Release date: September 2022
  • Publisher(s): Manning Publications
  • ISBN: 9781617299599