Go is a statically typed, compiled programming language designed at Google. It is syntactically similar to C, but with memory safety, garbage collection, structural typing, and CSP-style concurrency.
Go Anti-Patterns Overview
Not Handling Errors Properly
%w
verb to wrap errors for better context.Using Empty Interface (interface{}) Excessively
interface{}
) bypasses Go’s type system. Use specific interfaces that define the behavior you need instead.Not Using Context for Cancellation
context.Context
for cancellation, timeouts, and passing request-scoped values. This allows callers to cancel long-running operations.Returning Naked Returns
Using init() Functions Excessively
init()
functions run before main()
and can’t return errors. Use explicit initialization functions that can handle errors gracefully.Not Using Proper Package Organization
Using Global Variables
Not Using Interfaces for Testability
Using Pointers Unnecessarily
Not Using defer for Cleanup
defer
for cleanup operations to ensure they happen even if the function returns early due to an error.Not Using Structured Logging
Not Using Proper Error Types
error
interface for better error handling and more context.Not Using Channels Correctly
Not Using Context Timeout
context.WithTimeout
to set timeouts for external calls to prevent your application from hanging indefinitely.Not Using sync.WaitGroup Correctly
wg.Add
before starting goroutines and be careful with loop variables in goroutines.Not Using Proper Concurrency Patterns
Not Using Proper Error Wrapping
fmt.Errorf
with the %w
verb to wrap errors and add context while preserving the original error for checking with errors.Is
and errors.As
.Not Using Proper Testing Techniques
Not Using go.mod for Dependency Management