Continue Core Concepts of Go for API Development
Extend from the last blog post, here are more rules to live by when writing Go code for API development. Three more rules to go.
Rule 4: Dependencies point inward, not sideways
High-level packages depend on lower-level ones, never the other way around.
- api can depend on store
- store can depend on model
- model depends on nothing
Rule 5: Data shapes travel, behavior stays close to the data
Structs can move across layers, but logic should live where it naturally belongs.
Rule 6: Make illegal states unrepresentable
Design types so invalid situations can’t exist in the first place.
Examples:
Use type Slug string instead of string
Use nil pointers to represent absence intentionally
Prefer time.Time over strings
Avoid maps where a struct would express meaning
This rule dramatically reduces defensive code and clarifying questions later.
In Go, types are your first line of validation, not runtime checks.
