In Go (Golang), the go get
and go install
commands are both used to download and install packages, but they have distinct purposes and behaviors. Here’s a detailed explanation of each:
1. go get
- Purpose:
Thego get
command is used to download and install dependencies (Go modules or packages) from a remote repository (such as GitHub) to your$GOPATH
or Go module cache ($GOMODCACHE
), depending on whether you are working within a Go module. - Behavior:
- Download Dependencies: It fetches the source code of the specified package or module from a repository.
- Install Binaries: If you specify a package with an executable,
go get
will compile and install the binary into the$GOPATH/bin
directory (or$GOBIN
if set). - Modify
go.mod
: In the context of Go modules (Go 1.11 and above),go get
will also modify thego.mod
andgo.sum
files by adding or updating dependencies in the project. - Legacy Behavior: For older versions of Go (pre-1.17),
go get
was used to install packages or binaries directly, but this behavior has changed with newer versions of Go.
- Use Case:
- Installing or fetching new dependencies for your Go project or Go binary.
- Example:
go get github.com/gin-gonic/gin
2. go install
- Purpose:
Thego install
command is used to compile and install the Go code into the Go binary directory ($GOBIN
) or the default$GOPATH/bin
directory. This is typically used for installing executable programs from source code. - Behavior:
- Install Executables: If you have a Go program (not just a library),
go install
will compile the source code and place the resulting binary in the appropriate binary directory ($GOBIN
or$GOPATH/bin
). - With Go Modules: In the context of Go modules, it will not modify the
go.mod
orgo.sum
files. It is strictly focused on installing an executable. - Avoids Modifying Dependencies: Unlike
go get
,go install
will not alter your dependencies orgo.mod
file.
- Install Executables: If you have a Go program (not just a library),
- Use Case:
- When you want to install an executable Go program (e.g., a command-line tool) into your Go workspace or binary path.
- Example:
go install github.com/goharbor/harbor-cli/cmd/harbor
Key Differences
Feature | go get |
go install |
---|---|---|
Purpose | Fetches and installs dependencies from remote | Installs compiled binaries locally. |
Modifies go.mod ? |
Yes (modifies dependencies in go.mod ) |
No (does not modify go.mod or go.sum ) |
Installs Executables | Yes, for executables (Go 1.17 and before) | Yes, installs executables to $GOBIN |
Go Modules (Go 1.11+) | Fetches and installs Go modules dependencies | Installs Go binaries from source |
Use Case | To fetch packages and update dependencies | To install Go binaries (executables) |
Changes in Go 1.17 and Above
go get
no longer installs executables:
In Go 1.17 and above,go get
is no longer used to install executables. Instead,go install
is used to install executable Go binaries.- In Go 1.17+, to install an executable from a module, use
go install
:go install github.com/example/repo@latest
- In Go 1.17+, to install an executable from a module, use
Examples
Using go get
to fetch dependencies:
go get github.com/gin-gonic/gin
This fetches the gin
package and installs it as a dependency in your Go project. It also modifies the go.mod
file.
Using go install
to install a binary:
go install github.com/goharbor/harbor-cli/cmd/harbor
This compiles the harbor
command and installs the binary to $GOBIN
(or $GOPATH/bin
), making it available to run globally.
Summary
go get
: Used for fetching dependencies and, in older versions, installing binaries (pre-Go 1.17).go install
: Used for installing compiled Go executables and binaries, especially with Go 1.17+.
Let me know if you need more details!