Go Packages Rulebook
For reference summary of Go packages.
Published in
2 min readSep 26, 2017

About a Package
- It’s just a physical directory.
- Lets 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 the same name with another package if they live inside separate directories.
Package Types
- The 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 the organization.

Alright, that’s all for now. Thank you for reading so far.
Let’s stay in touch:
- 📩 Join my newsletter
- 🐦 Follow me on twitter
- 📦 Get my Go repository for free tutorials, examples, and exercises
- 📺 Learn Go with my Go Bootcamp Course
- ❤️ Do you want to help? Please clap and share the article. Let other people also learn from this article.