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. 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 from one or more packages.

Single concept

You should put 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 that 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.

How a Go package looks like?

“example” package. This is just a directory in your computer. A package resides in a directory.
All the source files in the example package directory have the same package name: example.
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 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.

Belongs to main package.
Output of 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 after fmt.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 to package 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 line to see what happens.

👉 Deepen your knowledge of Go packages:


Thanks for reading!

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.