Menu

Post image 1
Post image 2
1 / 2
0

Your Go Structs Are Leaking: 6 Encapsulation Fixes From a Security CLI

DEV Community·Bala Paranj·about 1 month ago
#ZjJavkP9
Reading 0:00
15s threshold

How returning pointers to internal slices, exposing mutable globals, and using pointer receivers on read-only methods let callers corrupt state in a Go CLI — and the exact fixes. Go doesn't have private or protected . It has exported (uppercase) and unexported (lowercase). This makes encapsulation feel optional. I audited my Go CLI project for encapsulation violations and found 10. Each one let a caller reach into a struct's internal state and mutate it — accidentally or intentionally. The mutations were silent: no error, no panic, no log. The struct just stopped working correctly. Here are the 6 encapsulation issues I found, with the exact before/after code from the fix. Pattern 1: Returning the Backing Slice The Violation // BEFORE: Caller gets a reference to the internal slice func ( c * Catalog ) List () [] ControlDefinition { return c .…

Continue reading — create a free account

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

Read More