Skip to content

Mutable Global State (ARCH5187)

Key go.quality.mutable-package-var · Severity warning · Category quality

Package-level mutable variables create hidden coupling, race conditions under concurrency, and brittle tests. Inject the state or make it constant.

var Config = map[string]string{}
func Set(k, v string) { Config[k] = v }
type Store struct{ data map[string]string }
func NewStore() *Store { return &Store{data: map[string]string{}} }
func (s *Store) Set(k, v string) { s.data[k] = v }

Encapsulate shared mutable state in a struct and inject it as a dependency.