Packages Cheatsheet

Visibility

  • Name: Capitalized = Exported (Public)
  • name: Lowercase = Unexported (Private)

Essential Commands

  • go mod init <path>: Start a new module.
  • go run <file>: Compile and run immediately.
  • go build: Create a static binary.
  • go install: Install binary to ~/go/bin.
  • go get <path>: Download dependency.
  • go mod tidy: Clean up unused dependencies.

Directory Rules

  • One package per directory.
  • Files in the same directory must share the same package name.
  • Package name usually matches the directory name.

Modules (go.mod)

  • Root of project contains go.mod.
  • Module path is the prefix for all internal packages.
  • Use replace for local development.

Design

  • Small, focused APIs.
  • Encapsulate internal logic.
  • Decouple from dependents.
  • No exports from package main.