Go install: Difference between revisions
(6 intermediate revisions by the same user not shown) | |||
Line 6: | Line 6: | ||
When used outside a project, <code>go install</code> [[#Install_External_Packages|installs external packages]] and possibly adds binaries to PATH. | When used outside a project, <code>go install</code> [[#Install_External_Packages|installs external packages]] and possibly adds binaries to PATH. | ||
When used inside a project, <code>go install</code> is similar to <code>[[go build#Overview|go build]]</code> except that is saves the compiled code in form of [[Go_Language#Object_File|object files]] and possibly binary executables, locally for each packages instead of discarding it. | When used inside a project, <code>go install</code> is similar to <code>[[go build#Overview|go build]]</code> except that is saves the compiled code in form of [[Go_Language#Object_File|object files]] and possibly binary executables, locally for each packages instead of discarding it. <code>go install</code> is used for building and publishing [[Go_Packages#Publishing_Packages|packages]] and [[Go_Modules#Publishing_Modules|modules]]. <font color=darkkhaki>If executed from a git workarea, there is underlying git interaction.</font> | ||
The shared flags describe here apply: {{Internal|Go_Tool_Shared_Flags#Overview|Shared Flags}} | |||
=Build and Install an Executable with <tt>go install</tt>= | =Build and Install an Executable with <tt>go install</tt>= | ||
Line 70: | Line 70: | ||
<font color=darkkhaki>TODO</font> | <font color=darkkhaki>TODO</font> | ||
=Install External Packages= | =Install External Packages= | ||
< | <code>go install</code> can be used as a dependency package management tool, to download and locally install packages under <code>$GOPATH/pkg/mod/cache/download/<package-import-path></code> and <code>$GOPATH/pkg/mod/<package-import-path></code>. The tool will recursively pull dependencies, as required. If the package exposes an executable binary, <code>go install</code> will compile it and install it under <code>$GOPATH/bin</code> or <code>$HOME/go/bin</code> if the <code>[[Go_Environment_Variables#GOPATH|GOPATH]]</code> environment variable is not set. | ||
<syntaxhighlight lang='bash'> | <syntaxhighlight lang='bash'> | ||
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest | go install google.golang.org/protobuf/cmd/protoc-gen-go@latest | ||
</syntaxhighlight> | </syntaxhighlight> | ||
The packages are stored under: <code>./pkg/mod/cache/download/github.com/alvaroloes</code> | The packages are stored under: <code>./pkg/mod/cache/download/<package-import-path></code>. Example: <code>./pkg/mod/cache/download/github.com/alvaroloes</code> | ||
<font size=-2> | <font size=-2> | ||
/Users/ovidiu/go/pkg/mod/cache/download/github.com/alvaroloes | /Users/ovidiu/go/pkg/mod/cache/download/github.com/alvaroloes | ||
Line 87: | Line 88: | ||
└── v1.1.2.ziphash | └── v1.1.2.ziphash | ||
</font> | </font> | ||
For another example, see: {{Internal|Oapi-codegen#Installation|oapi-codegen Installation}} | |||
==Removing Installed Packages== | |||
<syntaxhighlight lang='bash'> | |||
rm -rf ${GOPATH}/pkg/mod/cache/download/github.com/deepmap | |||
rm -rf ${GOPATH}/pkg/mod/github.com/deepmap | |||
rm ${GOPATH}/bin/oapi-codegen | |||
</syntaxhighlight> |
Latest revision as of 00:16, 26 January 2024
Internal
Overview
go install
can be used within a project, and independent of any project.
When used outside a project, go install
installs external packages and possibly adds binaries to PATH.
When used inside a project, 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. go install
is used for building and publishing packages and modules. If executed from a git workarea, there is underlying git interaction.
The shared flags describe here apply:
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
Install External Packages
go install
can be used as a dependency package management tool, to download and locally install packages under $GOPATH/pkg/mod/cache/download/<package-import-path>
and $GOPATH/pkg/mod/<package-import-path>
. The tool will recursively pull dependencies, as required. If the package exposes an executable binary, go install
will compile it and install it under $GOPATH/bin
or $HOME/go/bin
if the GOPATH
environment variable is not set.
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
The packages are stored under: ./pkg/mod/cache/download/<package-import-path>
. Example: ./pkg/mod/cache/download/github.com/alvaroloes
/Users/ovidiu/go/pkg/mod/cache/download/github.com/alvaroloes └── enumer └── @v ├── list ├── v1.1.2.info ├── v1.1.2.lock ├── v1.1.2.mod ├── v1.1.2.zip └── v1.1.2.ziphash
For another example, see:
Removing Installed Packages
rm -rf ${GOPATH}/pkg/mod/cache/download/github.com/deepmap
rm -rf ${GOPATH}/pkg/mod/github.com/deepmap
rm ${GOPATH}/bin/oapi-codegen