liveBook · Manning

Main package

As explained in Learn Go with Pocket-Sized Projects

The main package is the special Go package that defines an executable program's entry point.

The main package defines an executable Go program. A Go file begins with its package name; when that name is main, the compiler looks there for the special main function, which runs when the program starts. A small executable can keep main.go in the project root, while several binaries commonly use cmd/{binary_name}/main.go. section 2.1.1 section 6.1

Listing 2.1 main.go
package main

import "fmt"

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

A complete main.go example shows the package declaration, import, entry-point function, and executable output together. section 2.1.1

A minimal executable follows this structure:

  • Declare package main at the top of the Go file. section 2.1.1
  • Define func main() as the program's entry point. section 2.1.1
  • Place a single binary's main.go at the project root; organize multiple binaries under cmd/{binary_name}/. section 6.1

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains package declaration, compiler recognition, function execution, and binary layout without developing package design further.
Examples
Uses a Hello World executable and a money-converter command-line tool to demonstrate main package placement.
What you can do after
Build it from scratch by declaring package main and implementing func main() in an executable Go project.
How it connects
Connects packages, the main function, imports, modules, and command-line tools.

Also explained in

Compare how 2 books explain main 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.