Day 006 — Packages & Modules¶
Month 1 · Week 1 · ⬅ Day 005 · Day 007 ➡ · Journal index
🎯 Learning Objective¶
Organize code into a reusable package, understand exported vs unexported names, and run a first test.
📚 Topics¶
- Packages, modules (
go.mod), import paths, exported identifiers, doc comments,go testbasics
📖 Reading / Sources¶
- Learning Go ch.10 (modules/packages)
- How to Write Go Code
- Effective Go — Names, Commentary
📝 Notes¶
- A package is a directory of
.gofiles sharingpackage <name>(conventionally = directory name). A module is a tree of packages with ago.modat the root. - Capitalization is access control: identifiers starting with an uppercase letter are exported (public to importers); lowercase are package-private. No
public/privatekeywords. - Import by module path + subdir:
import "github.com/nabin747/go-from-zero/examples/mathutil". - Doc comments: a comment immediately above an exported symbol, starting with the symbol's name, becomes its documentation (
go doc, pkg.go.dev). package mainbuilds an executable; any other name builds a library.- First taste of testing:
*_test.go,func TestXxx(t *testing.T), run withgo test ./.... Used table-driven style (preview of Month 2).
💻 Code Examples¶
package mathutil
// Max returns the larger of a and b.
func Max(a, b int) int { if a > b { return a }; return b } // exported (uppercase)
func clampNeg(n int) int { … } // private (lowercase)
Full code:
examples/mathutil/· Test:go test ./examples/mathutil
🏋️ Exercises / Practice¶
| Exercise | Status | Link |
|---|---|---|
Extract mathutil package (Max/Min/Abs/IsPrime/Sum) |
✅ | examples/mathutil |
| Table-driven tests for it | ✅ | examples/mathutil/mathutil_test.go |
🐛 Mistakes Made¶
- Named a function
max(lowercase) then tried to call it from another package — unexported, so it wasn't visible. Renamed toMax. - Ran
go testin the wrong directory (no Go files) — must be inside the module / use./....
❓ Open Questions¶
- When to split into multiple packages vs keep flat? (Answer: split by responsibility, not by type; avoid premature packages — keep it flat until it hurts.)
🧠 Active Recall¶
- Q: How do you make a function usable by other packages?
A
Start its name with an uppercase letter (export it). - Q: What distinguishes a module from a package?
A
A module is a collection of packages versioned together, rooted at ago.mod; a package is one directory of related.gofiles.
🪶 Feynman Reflection¶
Folders of Go code are "packages"; a project of packages with a go.mod is a "module." Whether something is public is decided by ONE rule: capital first letter = visible to others.
🕳️ Knowledge Gaps¶
- Internal packages (
internal/) and semantic-version import rules — deferred to project work (Month 4).
✅ Summary¶
I can structure code into a documented, tested, reusable package and control its visibility.
⏭️ Next Steps / Prep for Tomorrow¶
- Day 007: Week 1 review + closed-book recall.
| Time spent | Difficulty | Confidence |
|---|---|---|
| 100 min | 🟦🟦⬜⬜⬜ | 🟦🟦🟦⬜⬜ |
Suggested commit: refactor(examples): extract mathutil package with tests (day 006)