Go install

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

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:

Shared Flags

go install is used for building and publishing packages and modules.

If executed from a git workarea, there is underlying git interaction.

go install is also used for locally installing external packages.

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

What exactly does this do? It seems it installs binaries in ${GOPATH}/bin and packages under ${GOPATH}/pkg

go install google.golang.org/protobuf/cmd/protoc-gen-go@latest

The packages are stored under: ./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