This document outlines key code conventions followed in this project.
- Avoid utility packages
Do not use generic utility package names likebase
,util
, orcommon
. For more context, see Dave Cheney's post explaining why. - Error wrapping
Errors should always be wrapped withfmt.Errorf
with the%w
directive. The wrapped message must provide enough context to help identify the error’s source at a glance. Avoid prefixes like "failed to" or "error" in messages, as they add no meaningful context.- Bad Example
A generic message that doesn’t provide specific context:if _, err := os.Create(configFileName); err != nil { return fmt.Errorf("failed to create file: %v", err) }
- Good Example
A more specific message that clarifies the error’s context:if _, err := os.Create(configFileName); err != nil { return fmt.Errorf("create config file: %w", err) }
- Bad Example
- Struct constructors
Struct constructors are specifically created for use withinapp.go
andmain.go
. They may initialize dependency fields with meaningful defaults and use static variables from other packages. An implication of this is that public tests should use constructors to initialize structs, and private tests may create a struct manually.