Go install
Internal
Overview
go install
is similar to go build
except that is saves the compiled code in form of object files and possibly binary executables, locally for each packages instead of discarding it. The shared flags describe here apply:
go install
is used for building and publishing packages and modules.
If executed from a git workarea, there is underlying git interaction.
Build and Install an Executable with go install
go install [options] <main-package-import-path>
Note that depending on whether the package is part of module or not, the main package import path may include the module path.
The name of the executable is given by the last segment of the main package import path and cannot be changed with -o
option, as in the case of the go build
command. Because of this detail, go build
is probably a better way to build executables, if you want to change the name of the executable.
The executable is placed under ${GOBIN}
directory if the GOBIN
environment variable is set, or under ${GOPATH}/bin
otherwise. The default value for ${GOPATH}
is ~/go
. For more details see GOPATH
.
Package without Module Support
In case of a package without module support, with the go
tool configured in GOPATH mode (GOPATH
configured to include the parent of the src
directory and GO111MODULE
set to "auto"), and with this directory layout:
. └─ src └─ a └─ b └─ c └─ main.go # package main
the executable is built with:
go install a/b/c
The executable will be named "c", based on the last segment of the package import path and will be place in a directory determined by the rules described above.
Unlike go build
, go install
does not accept an -o
option to change the name of the executable.
Package within a Module
An a/b/c
main package with a similar layout, but this time included within an example.com
module
. ├─ a │ └─ b │ └─ c │ └─ main.go # package main └─ go.mod
can have its executable built with:
go install example.com/a/b/c
The executable is named based on the last sentiment of the package import path and it placed in a directory determined by the rules described here.