Why this matters
Prevents accidental use of partial data and clarifies that the result is invalid when an error occurs.
When returning (T, error), return the zero value for T (or nil for pointers/slices/maps) whenever error != nil.
Prevents accidental use of partial data and clarifies that the result is invalid when an error occurs.
Side-by-side examples engineers can pattern-match during review.
func Find(id string) (User, error) {
u := User{ID: id}
return u, fmt.Errorf("not found") // partial value
}func Find(id string) (User, error) {
var zero User
return zero, fmt.Errorf("not found")
}return result, err // result is non-zerovar z T; return z, errFrom the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.