Canonical guidance

Use when

Avoid

Preferred pattern

const maxInt = int(^uint(0) >> 1)

func add(a, b int) (int, error) {
	if b > 0 && a > maxInt-b {
		return 0, errors.New("overflow")
	}
	return a + b, nil
}

Anti-pattern

Explanation: This anti-pattern is tempting because the math looks small and ordinary, but overflow often shows up exactly in boundary calculations.

Why

Related pages

Sources