We’ll write, compile, and run an executable program that prints the title of this book to the console. This tiny program introduces packages and modules. We’ll discuss packages and implement the program, initialize a new Go module, and run our program. To avoid surprises, you should use the same module you'll initialize in section A.3 throughout the book’s examples while following along.
A.1 Packages
A package is a set of Go source code files in the same directory. Every Go file in a package directory must declare the same package name.
- Packages named
main
are for building executable programs and cannot be imported from other packages for reuse. - Packages named anything but
main
can be imported from other packages.
NOTE Executable programs consist of a main
package and a main
function (which Go uses as the program’s entry point).
Go compiles all files within a package as a single unit, compiling separate packages concurrently to speed the compilation process and finally linking compiled packages into a final binary. Packages form firm boundaries, encapsulating internals and describing how other packages can interact with them.
- Package
main
is the entry point to our executable program. - Package
book
is an importable package. ItsTitle
function returns this book’s title.