Go Packages Rulebook
For reference summary of Go packages.


About a Package
- It’s just a physical directory.
- Let’s you organize your code.
- Enables reusability by allowing you to use other packages.
- Helps to enable code encapsulation.
- Represents a single concept.
- Small and many.
Package Rules
Source Files
- All code written in Go belongs to a package.
Package Directory
- Directory names and the package names should be the same.
- All source files in a directory belong to only one package.
- You can’t split a package into more than one directory.
- Packages live inside working space directory named
$GOPATH/src.
Identity
- A package can have a same name with another package it they live inside separate directories.
Package Types
- Main package is the entry-point package for executable programs.
- Library packages are only reusable packages and don’t contain main package, and an entry-point (mostly).
Functions and Variables
- In a Go package, there is no such thing as local variables or functions only belong to one source code file.
Importing
- Packages are imported with import keyword. Standard library packages can be imported by its name only (rather than full path). When importing other packages, it’s a good idea to supply the full path.
- For simple programs, you can only have
package main. For bigger programs, you may define your own packages. - Packages are imported only once. You can import the same package in many packages, and, it will be imported only once.
Sub-packaging
- No support.
- You can put packages inside other package’s directories to weakly imitate sub-packaging for organization.
To learn more about packages, see my other tutorials.


I’m also creating an online course for Go → Join to my newsletter ←
“ Let’s stay in touch weekly for new tutorials and tips “


My twitter — @inancgumus — I mostly tweet about Go.