liveBook · Manning

Package

As explained in Go in Action, Second Edition

A package is a Go compilation and namespace unit that groups related declarations and controls their visibility across source files.

A package is Go’s basic unit of code organization. It groups related functions, types, and values behind a boundary that determines what other packages can use. A module can contain multiple packages, each imported separately. section 2.4.5

An executable and a library call

Declare an executable’s files with `package main` and provide a `main()` function; together they give the compiler an entry point. To call an imported package, use its name as a qualifier: `fmt.Println("Found", numSpaces+1, "words")`. The capital P matters: identifiers beginning with an uppercase letter are exported for use by importers. section 2.4.5 section 2.4.11

Name other packages for their purpose, and keep dependencies flowing in one direction: the compiler rejects direct or indirect import cycles. A small application may need little structure. Larger projects can put separate executables under `cmd` and restrict externally importable code with `internal`. section 11.1.1

Growing an RSS application

An RSS reader can separate a shared domain model, persistent storage, a REST API, and a background worker. Packages provide the local code boundaries; modules group packages into independently managed components, and a workspace coordinates local development across those modules. section 11.0 section 11.4

How Go in Action, Second Edition covers it

How deep it goes
Explains declarations, exported identifiers, executable entry points, import cycles, and layout conventions without prescribing one application structure.
Examples
Uses `package main`, `fmt.Println`, conventional project directories, and a multimodule RSS reader.
What it assumes
Assumes familiarity with functions and imports while explaining package declarations and export naming.
What you can do after
Build it from scratch by choosing package boundaries, exposing needed identifiers, and avoiding import cycles.
How it connects
Connects packages to modules, workspaces, imports, interfaces, and dependency inversion.

Also explained in

Compare how 7 books explain package →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.