Mutable Global State (ARCH5187)
Ce contenu n’est pas encore disponible dans votre langue.
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.
Incorrect
Section titled “Incorrect”var Config = map[string]string{}
func Set(k, v string) { Config[k] = v }Correct
Section titled “Correct”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.