Go install: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 6: Line 6:


<font color=darkkhaki>If executed from a git workarea, there is underlying git interaction.</font>
<font color=darkkhaki>If executed from a git workarea, there is underlying git interaction.</font>
=Build and Install an Executable with <tt>go install</tt>=
<syntaxhighlight lang='bash'>
go install [options] <main-package-import-path>
</syntaxhighlight>
Note that depending on whether the package is part of module or not, the main package import path may include the [[Go_Modules#Module_Path_.28Module_Name.29|module path]].
The name of the executable is given by the last segment of the main package import path and cannot be changed with <code>-o</code> option, as in the case of the <code>[[go build#Overview|go build]]</code> command. Because of this detail, <code>go build</code> is probably a better way to build executables, if you want to change the name of the executable.
<span id='Executable_Placement'></span>The executable is placed under <code>${GOBIN}</code> directory if the <code>[[Go_Environment_Variables#GOBIN|GOBIN]]</code> environment variable is set, or under <code>${GOPATH}/bin</code> otherwise. The default value for <code>${GOPATH}</code> is <code>~/go</code>. For more details see <code>[[Go_Environment_Variables#GOPATH|GOPATH]]</code>.
==<span id='install_exec_PwMS'></span>Package without Module Support==
In case of a package without module support, with the <code>go</code> tool configured in [[Go_Language_Modularization#Module-Aware_or_GOPATH_Mode|GOPATH mode]] (<code>GOPATH</code> configured to include the parent of the <code>src</code> directory and <code>GO111MODULE</code> set to "auto"), and with this directory layout:
<font size=-2>
.
└─ src
    └─ a
       └─ b
        └─ c
            └─ main.go <font color=teal># package main</font>
</font>
the executable is built with:
<syntaxhighlight lang='bash'>
go install a/b/c
</syntaxhighlight>
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 [[#Executable_Placement|above]].
Unlike <code>go build</code>, <code>go install</code> does not accept an <code>-o</code> option to change the name of the executable.
==<span id='install_exec_PwM'></span>Package within a Module==
An <code>a/b/c</code> main package with a similar layout, but this time included within an <code>example.com</code> module
<font size=-2>
.
├─ a
│  └─ b
│    └─ c
│        └─ main.go <font color=teal># package main</font>
└─ go.mod
</font>
can have its executable built with:
<syntaxhighlight lang='bash'>
go install example.com/a/b/c
</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]].

Revision as of 00:49, 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:

Shared Flags

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.