LinuxCommandLibrary

go-get

Download and install Go packages

TLDR

Add a specified package to go.mod in module-mode or install the package in GOPATH-mode

$ go get [example.com/pkg]
copy

Modify the package with a given version in module-aware mode
$ go get [example.com/pkg]@[v1.2.3]
copy

Remove a specified package
$ go get [example.com/pkg]@[none]
copy

SYNOPSIS


go get
[-d] [-t] [-u] [-v] [-x] [build flags] [packages]

PARAMETERS


-d

    
Instructs
go get
to download the source code of the packages and their dependencies, but not to build or install them.


-t

    
When used with
-u
, considers modules needed for testing (i.e., listed in
require
directives of the
go.mod
file with a
// indirect
comment or in
go.mod
's
require
blocks that are only used for tests).


-u

    
Updates packages to use newer minor or patch versions when available. Without this flag,
go get
prefers existing versions that satisfy the module's requirements.


-v

    
Enables verbose output, showing details about the download and resolution process.


-x

    
Prints the commands being executed, which is useful for debugging.


[build flags]

    
Various build-related flags can be passed through, such as
-mod=readonly
(to prevent modifications to
go.mod
), or flags controlling compilation behavior.


[packages]

    
One or more package paths (e.g.,
github.com/some/module
), or
.
to refer to the current module. Specifies which packages/modules to operate on.

DESCRIPTION

The
go get
command is a crucial subcommand of the Go programming language toolchain, responsible for downloading and managing Go packages and modules. Its primary function is to fetch source code from remote repositories, such as GitHub or other version control systems, and resolve their dependencies.

Historically,
go get
downloaded packages directly into the user's
GOPATH
workspace. However, with the introduction of Go Modules in Go 1.11 (and becoming the default in Go 1.13), its behavior significantly evolved. In a module-aware environment,
go get
is primarily used to add, update, or remove module dependencies recorded in the
go.mod
file. When run without arguments inside a module, it updates all dependencies. When given a package path, it adds or updates that specific dependency.

While it can still be used to download source code or install executable commands, its role for installing executables has largely been superseded by
go install
since Go 1.16, which provides more explicit control over versioning and installation paths.
go get
now focuses more on managing the dependency graph for your project, ensuring that your
go.mod
and
go.sum
files accurately reflect the required modules and their checksums. It handles transitive dependencies, ensuring your project has all necessary components to build and run.

CAVEATS

The
go get
command requires a properly installed Go toolchain. Its behavior significantly changed with the introduction of Go Modules (Go 1.11+), making it crucial to understand the context (GOPATH vs. Modules) in which it's being used. In a module-aware project, it will modify your
go.mod
and
go.sum
files to reflect dependency changes. An active internet connection is typically required to fetch remote modules.

MODULE-AWARE MODE

In projects using Go Modules (indicated by a
go.mod
file),
go get
acts as a dependency manager. It resolves and downloads necessary module versions, updating the
go.mod
and
go.sum
files. When run without arguments in a module root, it updates all direct and indirect dependencies to their latest compatible versions.

INSTALLING EXECUTABLES

While
go get
historically installed executables (e.g.,
go get github.com/user/tool
), its role for this purpose has largely been deprecated since Go 1.16. The recommended command for installing Go programs into your
GOBIN
(or
GOPATH/bin
) is now
go install
, which offers more explicit control over versions and ensures correct binary placement, for example,
go install github.com/user/tool@latest
.

HISTORY

The
go get
command has been a fundamental part of the Go toolchain since its early days. Initially, its primary role was to fetch and install packages into the user's
GOPATH
workspace, often building and caching them.

A significant evolution occurred with Go 1.11 and the advent of Go Modules, becoming the default in Go 1.13. In the module system,
go get
transformed from a general-purpose package installer to a focused dependency manager. Its main function became updating the
go.mod
file to reflect required module versions.

Further refinement in Go 1.16 saw the
go install
command become the preferred method for installing executable Go programs, separating that concern from
go get
's primary role of managing module dependencies.

SEE ALSO

go(1), go build(1), go mod(1), go install(1)

Copied to clipboard