Go Tool: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 18: Line 18:
==<span id='Build_an_Executable_with_go_build'></span><span id='Package_without_Module_Support'></span><span id='Package_within_a_Module'></span><tt>build</tt>==
==<span id='Build_an_Executable_with_go_build'></span><span id='Package_without_Module_Support'></span><span id='Package_within_a_Module'></span><tt>build</tt>==
{{Internal|go build#Overview|<tt>go build</tt>}}
{{Internal|go build#Overview|<tt>go build</tt>}}
The <code>build</code> command compiles one or more packages. If a [[Go_Packages#The_main_Package|"main" package]] is among the arguments, the tool creates an executable as described [[#Build_an_Executable_with_go_build|below]]. The following flags apply:{{Internal|Go_Tool_Shared_Flags#Overview|Shared Flags}}
<font color=darkkhaki>TO DEPLETE: [[Go_Commands_-_build]]</font>
===Build an Executable with <tt>go build</tt>===
<syntaxhighlight lang='bash'>
go build [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 executable is placed in the directory <code>go build</code> 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 <code>-o</code> command line option. The argument of <code>-o</code> 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.
====<span id='build_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 build 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 the directory <code>go build</code> was executed from. To change the name of the executable, use the <code>-o</code> option:
<syntaxhighlight lang='bash'>
go build -o blue a/b/c
</syntaxhighlight>
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>


==<span id='Build_and_Install_an_Executable_with_go_install'></span><span id='Package_without_Module_Support_2'></span><span id='Package_within_a_Module_2'></span><span id='Build_and_Install_Package_Object_Files'></span><span id='Package_without_Module_Support_3'></span><span id='Package_within_a_Module_3'></span><tt>install</tt>==
==<span id='Build_and_Install_an_Executable_with_go_install'></span><span id='Package_without_Module_Support_2'></span><span id='Package_within_a_Module_2'></span><span id='Build_and_Install_Package_Object_Files'></span><span id='Package_without_Module_Support_3'></span><span id='Package_within_a_Module_3'></span><tt>install</tt>==

Revision as of 00:58, 3 October 2023

External

Internal

Overview

go is a command line tool with multiple uses: package manager, build tool and test driver. go manage packages in workspaces, query metadata about packages, print documentation, build, format, download, test, etc.

Shared Flags

Shared Flags

Commands

Help

go help <command>
go help build

build

go build

install

go install

run

The run command compiles the specified packages or files by delegating to go build and then runs the executable. There must be a main for an executable to be generated.

cd $PROJECT_DIR
go run ./src/main/main.go some-arg-1 some-arg-2

The first argument that does not end in .go is assumed to be the beginning of the list of command line arguments of the executable.

The shared flags described here apply:

Shared Flags

clean

The shared flags described here apply:

Shared Flags

-cache

Clean the build cache:

go clean -cache

-fuzzcache

Clean the fuzz cache:

go clean -fuzzcache

doc

The doc command prints documentation for a package or a package member:

go doc time
go doc time.Since

fmt

The fmt ("format") command formats source code files.

get

The get command downloads packages and installs them. The shared flags described here apply:

Shared Flags

list

The list command lists all installed packages. The shared flags described here apply:

Shared Flags

test

The test command runs tests. The shared flags described here apply:

Shared Flags

For more details, see:

Go Testing

env

The env command prints the effective values of the environment variables relevant to the tool chain.

go env GOOS GOARCH GOPATH

get

The get command retrieve and update packages.

TODO Addison-Wesley The Go Programming Language Section 10.7.2

mod

go mod

TO DEPLETE

Deplete: