Introduction to Go packages
What are Go packages, how to use them, and how to import them.
What is a Go package?
A package is a grouping unit for code and helps you to organize your program. It allows you to “pack” one or more source code files in a single unit and make them reusable.
If you’re coming from other languages, they’re like namespaces or packages. The difference is, there’s no sub-packaging, and the packages are small and many.
For example: If you have a code which does some calculation for weather statistics and is being used by more than one code in your program, you would put this calculation code in a separate package, maybe named as: statistics.
Any code written in Go belongs to a package
Go programs are composed of one or more packages.
Single concept
You should put the only related code into a package, and it’s important to name them properly.
For example: Go’s zip package provides only one concept: zipping things. Or, http package provides only one concept again: client/server interactions of http. Or, fmt package: Handling the formatting of input/output.
Contains zero or more functions and state
There can be a package that only contains one function such as calculating the tax ratio for some countries. Go packages don’t need to be big as in some other languages.
Reusable
Other packages can only use a package’s exported functions and data. Also, a package can use its own functionalities whether they’re exported or not.
Imported only once
You can import the same package in many packages, and, it will be imported only once.
No packages?
Without packages there would be almost no code reusability, and organization.
Take a look at Go’s standard library. Without packages, how would you use Go’s massive standard library? Functionalities would be scattered everywhere, and it’d be impossible to work with.
What does a Go package look like?
Physically, a package is just a directory. Directory names and the package names should be the same.
In a package directory, all source files belong to only one package. In the example above, package
keyword declares that example.go
belongs to the example package. A source code file should have a package
, always.
Importing packages
import
keyword allows you to use other packages as seen in the examples
Let’s try something basic
This file belongs to package main. You can run this code here. I ran it through my command line by typing: go run main.go
.
An Exercise for You
To really learn something, you need to try it on your own.
- Open the sample code and import time package by typing
import time
. - Display the time by typing
fmt.Println(time.Now())
in a new line afterfmt.Println(“i am main.”)
line and run the code. - Code for the exercise is here. Compare it with yours. Now, delete all the lines and type the program from your memory by yourself, without looking to the exercise sample.
Another Exercise for You
- Open the code and change
package main
topackage notmain
and run it by clicking the Run button above - Then, look at the error message at the bottom. That’s because every runnable program should have at least one main package.
- Try to change other things like, delete
import fmt
to see what happens.
👉 Deepen your knowledge of Go packages:
- How to use packages
- Organize your code with packages
How to design a Go program with packages. - How to decouple your packages
How to increase code maintainability with packages. - Special Packages and Directories
About internal, main, library and vendor directories. - Fix your $GOPATH and $GOROOT
How to configure them and what are they. - Go Packages Rulebook
Summarizing the rules for a reference.
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.