Reputation: 5720
After playing with the go
tool for a while, it looks like go get
:
a piece of software, while go install
simply
it. In this case, why does the go install
command exist, since go get
supersedes it?
Upvotes: 169
Views: 133687
Reputation: 23
This is from go help get:
In earlier versions of Go, 'go get' was used to build and install packages. Now, 'go get' is dedicated to adjusting dependencies in go.mod. 'go install' may be used to build and install commands instead. When a version is specified, 'go install' runs in module-aware mode and ignores the go.mod file in the current directory.
I understood that if you want to use some package as a CLI use e.g: go install github.com/golang-migrate/migrate/v4/cmd/migrate@latest
it will install it locally on your machine, so you can easily it use locally as just usual command. On the other hand, if you use e.g: go get golang.org/x/crypto
, it will download it to your go.mod file in your project. That's how I understood.
Upvotes: 1
Reputation: 180
Please refer to this - https://go.dev/doc/go-get-install-deprecation
Referencing -
Starting in Go 1.17, installing executables with go get is deprecated. go install may be used instead.
In Go 1.18, go get will no longer build packages; it will only be used to add, update, or remove dependencies in go.mod. Specifically, go get will always act as if the -d flag were enabled.
Upvotes: 3
Reputation: 3115
from doc:
Starting in Go 1.17, installing executables with go get is deprecated. go install may be used instead.
In Go 1.18, go get will no longer build packages; it will only be used to add, update, or remove dependencies in go.mod. Specifically, go get will always act as if the -d flag were enabled.
Upvotes: 6
Reputation: 1324606
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 causesgo install
to build and install packages in module-aware mode, ignoring thego.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 ofgo get
to build and install packages is deprecated.
In a future release, the-d
flag will always be enabled.
In June 2022 (Go 1.18 right before 1.19), Chris Siebenmann details "a limitation on what 'go install
' can install".
It involves a module with a replace
directive:
If you clone the repository and run '
go install
' inside it, everything works and you wind up with agospy
binary in your$HOME/go/bin
.However, as we see here '
go install ...@latest
' works differently enough that the replace directive causes this error.
That triggers the error message:
go: github.com/monsterxx03/gospy@latest (in github.com/monsterxx03/[email protected]):
The go.mod file for the module providing named packages contains one or
more replace directives. It must not contain directives that would cause
it to be interpreted differently than if it were the main module.
The help for 'go install' explicitly says about this mode:
No module is considered the "
main
" module.If the module containing packages named on the command line has a
go.mod
file, it must not contain directives (replace
andexclude
) that would cause it to be interpreted differently than if it were the main module.
The module must not require a higher version of itself.
See:
cmd/go
: go install cmd@version
errors out when module with main
package has replace
directivecmd/go
: go install
should install executables in module mode outside a modulex/tools/gopls
: cannot install gopls
v0.6.8 (illustrates the issue)Recommendation:
switch to using Go workspaces for local development and drop the
go.mod
replace
directives entirely.
As mentioned in "Get familiar with workspaces" by Beth Brown:
Previously, to add a feature to one module and use it in another module, you needed to either publish the changes to the first module, or edit the
go.mod
file of the dependent module with areplace
directive for your local, unpublished module changes.In order to publish without errors, you had to remove the
replace
directive from the dependent module’sgo.mod
file after you published the local changes to the first module.With Go workspaces, you control all your dependencies using a go.work file in the root of your workspace directory.
Thego.work
file hasuse
andreplace
directives that override the individualgo.mod
files, so there is no need to edit eachgo.mod
file individually.
Upvotes: 25
Reputation: 10173
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;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: six 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
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 ofgo get
to build and install packages is deprecated. In a future release, the-d
flag will always be enabled.
Upvotes: 145
Reputation: 11702
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
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.
Upvotes: 63