Go build: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 34: Line 34:
</syntaxhighlight>
</syntaxhighlight>
This will create an executable named "blue".
This will create an executable named "blue".
==<span id='build_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 build [-o blue] example.com/a/b/c
</syntaxhighlight>
The exact directory within the project <code>go build</code> is run from it does not matter, the tool will locate <code>go.mod</code> and will use relative paths to that. The only aspect influenced by where the compilation tool runs is where it will place the final executable.
===Executable Name===
If no <code>-o</code> flag is used, the name of the executable will be given by the name of the directory that contains the Go file that belongs to the <code>main</code> package. This behavior can be overridden with <code>-o</code>.
=Options=
=Options=
==<tt>-C</tt>==
==<tt>-C</tt>==
Change to the specified directory before running the command. This is the final executable result will be placed.
Change to the specified directory before running the command. This is the final executable result will be placed.

Revision as of 20:32, 14 December 2023

Internal

Overview

The go build command compiles one or more packages. If a "main" package is among the arguments, the tool creates an executable as described below. The following flags apply:

Shared Flags

TO DEPLETE: Go_Commands_-_build

Build an Executable with go build

go build [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 executable is placed in the directory go build is executed from.

The name of the executable is given by the last segment of the main package import path, and it can be configured with the -o command line option. The argument of -o can be the new name of the executable file, in which case the executable will be written in the current directory, or an absolute or relative path, in which case the executable, named after the last segment of the path, will be written at the given path.

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 build a/b/c

The executable will be named "c", based on the last segment of the package import path and will be place in the directory go build was executed from. To change the name of the executable, use the -o option:

go build -o blue a/b/c

This will create an executable named "blue".

Options

-C

Change to the specified directory before running the command. This is the final executable result will be placed.