Tuesday, January 21, 2025
HomeQ&AWhat is the difference between go get and go install?

What is the difference between go get and go install?

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:
    The go 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 the go.mod and go.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:
    The go 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 or go.sum files. It is strictly focused on installing an executable.
    • Avoids Modifying Dependencies: Unlike go get, go install will not alter your dependencies or go.mod file.
  • 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)
See also  How much is 6 oz of yogurt?

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
      

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.

See also  What does La Cerveza Mas Fina mean in English?

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+.
See also  16 ounces sour cream equal how many cups of sour cream?

Let me know if you need more details!

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x