π§ Migration Checklist β From Java (OOP) to Go π¦«
π Table of Contents
- 1. π Design & Architecture
- 2. π§ Structs & Interfaces
- 3. π§ Mindset & Philosophy
- 4. π§΅ Memory & Pointers
- 5. β‘ Error Handling
- 6. π§ͺ Testing & Mocking
- 7. π Concurrency Tips
- 8. π¦ Project Organization
1. π Design & Architecture
-
Prefer composition over inheritance β embed structs instead of subclassing.
-
Design small, focused interfaces that describe behavior, not objects.
-
Organize code by domain or feature, not by technical layer.
-
Avoid creating βGod objectsβ β structs should have clear, limited responsibility.
-
Keep packages cohesive; avoid circular dependencies.
2. π§ Structs & Interfaces
-
Use structs for data and behavior when needed, not as βclasses.β
-
Add methods to structs only when they logically belong there.
-
Define interfaces at the consumer side, not the producer side.
-
Donβt write getters/setters unless they add real logic or validation.
-
Use factory functions (
NewX()
) instead of constructors.
3. π§ Mindset & Philosophy
-
Embrace simplicity and explicitness β Go avoids βmagic.β
-
Donβt over-engineer abstractions β solve real problems first.
-
Follow idiomatic Go: clear, readable, minimal.
-
Prefer composition, interfaces, and functions over deep hierarchies.
-
Read standard library code β itβs the best example of Go idioms.
4. π§΅ Memory & Pointers
-
Use pointers when modifying a struct or avoiding large copies.
-
Use values for small, immutable, or short-lived data.
-
Remember that maps and slices are already reference types.
5. β‘ Error Handling
-
Handle errors explicitly:
if err != nil { β¦ }
. -
Wrap errors with context using
fmt.Errorf("msg: %w", err)
. -
Avoid panic for control flow; use it only for truly exceptional cases.
-
Remember: no exceptions, no try/catch β explicit handling wins.
6. π§ͺ Testing & Mocking
-
Use interfaces for mocking only when necessary.
-
Keep tests simple and clear β Go favors integration tests.
-
Avoid test frameworks with heavy magic; use
testing
andtestify
.
7. π Concurrency Tips
-
Learn goroutines β theyβre cheap and safe for concurrent work.
-
Use channels for communication, not shared mutable state.
-
Prefer context cancellation for graceful shutdowns.
-
Avoid overusing sync primitives like
Mutex
; prefer message passing.
8. π¦ Project Organization
-
Use
cmd/
for main entry points. -
Use
internal/
for private code. -
Use
pkg/
for reusable libraries (if needed). -
Keep
main.go
minimal β wire dependencies explicitly. -
Store configs in
.env
or aconfig
package.