liveBook · Manning

Package

As explained in Learn Go with Pocket-Sized Projects

A Go package is a collection of related source files in one directory that share a package name and provide a scope for declarations.

A Go package groups related source files in one directory under a shared package name. It organizes applications and provides scope for functions, types, variables, and constants. section 4.1.1

Apply these package rules when structuring Go code:

  • Start every Go file with a package declaration, and keep files in the same directory under one package name. section 4.1.1
  • Prefer a short, lowercase, usually single-word package name; conventionally match it to the directory name. section 2.1.1 section 4.1.1
  • Export a symbol by starting its name with an uppercase letter; lowercase names remain inside the package. section 4.1.1
  • Keep exported APIs stable for compatibility while allowing unexported implementation details to change. section 4.1.1

A runnable main package

package main\n\nimport "fmt"\n\nfunc main() {\n fmt.Println("Hello world")\n}\n\nRun it with `go run main.go`. Larger projects can place reusable code in packages such as `gordle`, `pocketlog`, or `genericcache`, alongside `main.go` or at the module root. section 2.1.1 section 5.1 section 7.1.2

Listing 2.1 main.go
package main

import "fmt"

func main() {
    fmt.Println("Hello world")
}       

Inspect the complete minimal program, then compare its package declaration, import, and main function. section 2.1.1

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Defines package structure, naming, visibility, and API stability, but does not descend into compiler internals.
Examples
Demonstrates main.go, fmt, pocketlog, gordle, and genericcache through runnable code and project trees.
What it assumes
Assumes basic Go file and command-line familiarity while teaching package declarations through a first runnable program.
What you can do after
Build packages with deliberate names, directory layouts, exported APIs, and runnable entry points.
How it connects
Connects packages with modules, directories, imports, declarations, exported symbols, and backward compatibility.

Also explained in

Compare how 7 books explain package →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.