Everything you need to know about Packages in Go

A complete overview of package management and deployment in Go programming language

Uday Hiwarale
Published in
14 min readJul 20, 2018
(source: pexels.com)

Migrate to Go Modules

Go provides a new feature called Modules which provides flexibility to manage your project and dependencies with ease. If you are working on Go packages, then you should consider relocating your project to Go Modules.

If you are familiar to languages like Java or JavaScript on Node, then you might be quite familiar with packages. A package is nothing but a directory with some code files, which exposes different variables (features) from a single point of reference. Let me explain, what that means.

Imagine you have more than a thousand functions that you need constantly while working on any project. Some of these functions have common behavior. For example, toUpperCase and toLowerCase function transforms case of a string, so you write them in a single file (probably case.go). There are other functions which do some other operations on string data type, so you write them in a separate file.

Since you have many files which do something with string data type, so you created a directory named string and put all string related files into it. Finally, you put all of these directories in one parent directory which will be your package. The whole package structure looks like below.

├── string
| ├── case.go
| ├── trim.go
| └── misc.go
└── number
├── arithmetics.go
└── primes.go

I will explain thoroughly, how we can import functions and variables from a package and how everything blends together to form a package, but for now, imagine your package as a directory containing .go files.

Every Go program must be a part of some package. As discussed in Getting started with Go lesson, a standalone executable Go program must have package main declaration. If a program is part of the main…