Go install: Difference between revisions
Line 49: | Line 49: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
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 [[#Executable_Placement|here]]. | 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 [[#Executable_Placement|here]]. | ||
=Build and Install Package Object Files= | |||
==<span id='install_object_PwMS'></span>Package without Module Support== | |||
<code>[[Go_Environment_Variables#GOPATH|GOPATH]]</code> and <code>[[Go_Environment_Variables#GO111MODULE|GO111MODULE]]</code> should be configured as described in the [[#install_exec_PwMS|Package without Module Support]] executable <code>install</code> section, above. The package [[Go_Language#Object_File|object file]] is built and installed with: | |||
<syntaxhighlight lang='bash'> | |||
go install <package-import-path> | |||
</syntaxhighlight> | |||
The command will compile and install the object file under <code>${[[Go_Environment_Variables#GOPATH|GOPATH]]}/pkg/${[[Go_Environment_Variables#GOOS|GOOS]]}_${[[Go_Environment_Variables#GOARCH|GOARCH]]}/<import-path></code>. | |||
If the package specified as argument has dependency packages, <code>go install</code> will correctly locate and compile the source files for both the dependent package and all dependency packages identified walking the transitive dependency graph. If any of the dependencies fails to compile, the installation process will fail. However, only the object file corresponding to specified package will be written in <code>${GOPATH}/pkg</code>. To install the object files for the entire dependency chain, the packages have to be explicitly specified on the command line. | |||
If the <code>pkg</code> subdirectory does not exist, it will be created. The <code>go install</code> can be run from the <code>${GOPATH}</code> directory, or from some any other directory. As long the <code>GOPATH</code> and <code>GO111MODULE</code> are correctly set, the results will be the same. | |||
<code>[[Go_Environment_Variables#GOPATH|GOPATH]]</code> may list [[Go_Environment_Variables#Relative_Order|multiple colon-separated directories]]. As long as the package to be installed is declared under one of those directories, it will be correctly located and <code>go install</code> will write the corresponding object files under the <code>pkg</code> subdirectory of the corresponding root. If multiple <code>GOPATH</code> directories contain package with the same import path, only the first package will be processed. | |||
==<span id='install_object_PwM'></span>Package within a Module== | |||
<font color=darkkhaki>TODO</font> |
Revision as of 00:52, 3 October 2023
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.
Build and Install Package Object Files
Package without Module Support
GOPATH
and GO111MODULE
should be configured as described in the Package without Module Support executable install
section, above. The package object file is built and installed with:
go install <package-import-path>
The command will compile and install the object file under ${GOPATH}/pkg/${GOOS}_${GOARCH}/<import-path>
.
If the package specified as argument has dependency packages, go install
will correctly locate and compile the source files for both the dependent package and all dependency packages identified walking the transitive dependency graph. If any of the dependencies fails to compile, the installation process will fail. However, only the object file corresponding to specified package will be written in ${GOPATH}/pkg
. To install the object files for the entire dependency chain, the packages have to be explicitly specified on the command line.
If the pkg
subdirectory does not exist, it will be created. The go install
can be run from the ${GOPATH}
directory, or from some any other directory. As long the GOPATH
and GO111MODULE
are correctly set, the results will be the same.
GOPATH
may list multiple colon-separated directories. As long as the package to be installed is declared under one of those directories, it will be correctly located and go install
will write the corresponding object files under the pkg
subdirectory of the corresponding root. If multiple GOPATH
directories contain package with the same import path, only the first package will be processed.
Package within a Module
TODO