What is the difference between go get and go install?

Go

Go Problem Overview


After playing with the go tool for a while, it looks like go get:

  1. (optionally) downloads,
  2. compiles,
  3. and installs

a piece of software, while go install simply

  1. compiles
  2. and installs

it. In this case, why does the go install command exist, since go get supersedes it?

Go Solutions


Solution 1 - Go

go install is part of the workflow when working locally. Say you want to use a library, but for some reason a change is required. You would do:

  • go get -d library, which only downloads it;
  • make the change on the downloaded package;
  • go install library to install the local version.

As far as I know go get has no flags to indicate it should not download, so it can't replace go install here.

The same workflow is used when you develop a new package from scratch.

EDIT: 6 years later, Go 1.16 has updated and clarified the usage of go install and go get: https://tip.golang.org/doc/go1.16#modules

Solution 2 - Go

go get does two main things in this order:

  • downloads and saves in $GOPATH/src/<import-path> the packages (source code) named in the import paths, along with their dependencies, then

  • executes a go install

The -d flag (go get -d) instructs go get to stop after downloading the packages; that is, it instructs go get not to do go install


the difference:

go get // verify if packages need to be downloaded, download if needed then compile

go install // skip the part with packages download, just compile (this will throw an error if any packages are missing)


about GOPATH environment variable

The GOPATH environment variable is used by the Go tools. It must be set in order to be able to get, build and install packages, and it specifies the location of your workspace. It is likely the only environment variable you'll need to set when developing Go code.

Again, the GOPATH should not point to the Go installation, but rather to your workspace.

For example, on Windows, if you decide that your workspace is at c:\gowork\, you will need to set GOPATH value as c:\gowork

enter image description here

Your source code should be at c:\gowork\src\<some project folder>\ and after you run go get at command prompt from within c:\gowork\src\<some project folder>\ you will see the c:\gowork\bin\ and c:\gowork\pkg\ being created.

Solution 3 - Go

Note that go 1.16 (Q1 2021) will make that difference clearer, implemented with CL 266360 as part of issue 40276:

> go install now accepts arguments with version suffixes (for example, go install example.com/[email protected]).
This causes go install to build and install packages in module-aware mode, ignoring the go.mod file in the current directory or any parent directory, if there is one.
This is useful for installing executables without affecting the dependencies of the main module. > > go install, with or without a version suffix (as described above), is now the recommended way to build and install packages in module mode. > > go get should be used with the -d flag to adjust the current module's dependencies without building packages, and use of go get to build and install packages is deprecated.
In a future release, the -d flag will always be enabled.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionthiagowfxView Question on Stackoverflow
Solution 1 - GoBoppreHView Answer on Stackoverflow
Solution 2 - GoAlex BabanView Answer on Stackoverflow
Solution 3 - GoVonCView Answer on Stackoverflow