Menu

Post image 1
Post image 2
1 / 2
0

Global State in Go: 5 Kinds We Found, 3 We Eliminated, 2 We Kept

DEV Community·Bala Paranj·25 days ago
#pVQ7ORoZ
Reading 0:00
15s threshold

A mutable registry populated by init(), a session field leaking between evaluations, a validation limit set by a setter, an immutable singleton, and a cached detection result — how to tell which globals are dangerous and which are acceptable. Not all global state is bad. A var ErrNotFound = errors.New("not found") is global state. So is var controlIDPattern = regexp.MustCompile(...) . Nobody argues these should be parameters. The problem is when global state is mutable and shared across execution boundaries — when one function writes it and another reads it without explicit coordination. In a CLI, this means two evaluations in the same process see each other's side effects. In tests, it means parallel tests corrupt each other's state. We found 5 kinds of global state in a Go security CLI. Three were dangerous. Two were acceptable. Here's how we decided.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More