Skip to content

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 test basics

📖 Reading / Sources

📝 Notes

  • A package is a directory of .go files sharing package <name> (conventionally = directory name). A module is a tree of packages with a go.mod at the root.
  • Capitalization is access control: identifiers starting with an uppercase letter are exported (public to importers); lowercase are package-private. No public/private keywords.
  • 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 main builds an executable; any other name builds a library.
  • First taste of testing: *_test.go, func TestXxx(t *testing.T), run with go 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 to Max.
  • Ran go test in 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

  1. Q: How do you make a function usable by other packages?
    AStart its name with an uppercase letter (export it).
  2. Q: What distinguishes a module from a package?
    AA module is a collection of packages versioned together, rooted at a go.mod; a package is one directory of related .go files.

🪶 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)