Modules & Packages

Packages

Every Go application needs at least one package, that you can split your code in.

Drawing

Usually you will have a default package main. Theoretically it could be any package name, as long as all files belong to the same package.

You may also split your code into multiple packages.

Drawing

Importing own packages

// Your own packages
import "your-module/package-name"

// Third-party that were downloaded with `go get`
import "https://package-url.com"

Add packages locally

This downloads the package and stores globally on your system.

go get https://package-url.com

Install packages

Just like npm install run bellow to install the projects dependencies.

It fetches the current required version (defined in your go.mod or latest compatible)

It not listed uet, it adds the latest matching your current module's Go version and requirements.

It does not update transitive dependencies unless needed.

It fetches a version that works with the rest of your dependencies. No wild upgrades.

go get

Fetches the latest available version of that package and all its dependencies (upgrades them!)

Can cause major upgrades to transitive deps.

Will pull the latest package AND the latest of all its nested imports (could affect other parts of your codebase)

go get -u

Modules

A module consist of multiple packages, and it is the go.mod file in your project.

go mod init <path>

The path can be a name or dummy URL, or even a github repository URL.

Last updated