UNDERSTANDING DEPENDENCIES IN GOLANG

wafulakevin

Kevin Wafula

Posted on August 21, 2023

UNDERSTANDING DEPENDENCIES IN GOLANG

Golang relies on external packages. The packages are considered dependencies in your project. Go provides tools to manage this dependencies to allow you to replace or update during the life time of your project.

In this article we’ll discuss:
• Locating packages on pkg.go.dev
• Importing the packages
• Enable dependency tracking by adding your code to a module.
• Add and manage external packages to your module.
• How to upgrade and downgrade versions of the imported dependencies
• Golang commands used to make dependency changes

Dependencies as modules
Managing dependencies is a critical aspect of building projects in Go. Go provides tools that manage dependencies as modules that contains the imported packages. For efficient management of dependencies Golang provides:
• A documentation browser and a package search engine (pkg.go.dev).
• Go tools for upgrading, downgrading and managing package sources
• A decentralized system for publishing modules and retrieving their code. Developers use version numbers and repository to achieve this.

Locating and importing the packages
Search pkg.go.dev to find the packages that contain the functions you need.
Image description

Find the package path and copy. Paste the path in your import statement.

import "golang.org/x/sync/errgroup"

Enter fullscreen mode Exit fullscreen mode

If there’s more than one package:

import(
"golang.org/x/sync/errgroup"
"github.com/mitchellh/iochan"
)
Enter fullscreen mode Exit fullscreen mode

Dependency Tracking

To enable tacking and management of your dependencies you have to put your code in a module. This creates a go.mod file at the root of your of directory. All the dependencies you’ll add will be listed in the _*go.mod *_file.

Image description

The go.mod file looks like this:

Image description

Run

`go mod tidy`
Enter fullscreen mode Exit fullscreen mode

to add module requirements and sums. It also removes dependencies to the packages you’re no longer using.

Naming of Modules
To run go mod init you need to specify a module path which also serves as the module’s name. Naming modules in Go is required to take the following form: /. The prefix describes the module. In this case we’re using github.com as it is the best practice for modules you intend to publish for others to use. Descriptive text is the name of the project.
Reserved module prefixes
• example – used in Go documentation and tutorials as a module path i.e. example.com/kevin
• test – only used for a module whose code is used to locally test functions in another module i.e. go mod init test

Adding a Dependency
To add a dependency for a package in your module run go get . In the case of a specific dependency, you need to specify its module path. I.e. go get example.com/themodule .This command authenticates each module it downloads and makes sure it is unchanged. In case of any changes it will provide a security error.
You can also get a specific version of a module by specifying the version of the module.

`go get example.com/themodule@v1.4`
Enter fullscreen mode Exit fullscreen mode

or the latest version by

`go get example.com/themodule@latest`
Enter fullscreen mode Exit fullscreen mode

Checking for available updates
Use the go list command to display all your module dependencies. The command

`go list –m –u all`
Enter fullscreen mode Exit fullscreen mode

lists all the dependences of your current module with their latest version.

In the above article I sought to summarize all the important information regarding dependencies. There are other detailed aspects of dependencies like developing and testing against unpublished code and getting a specific commit using a repository Identifier that are covered in detail by Go official documentation.

💖 💪 🙅 🚩
wafulakevin
Kevin Wafula

Posted on August 21, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related