Go Tool: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(196 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* https://pkg.go.dev/cmd/go
=Internal=
=Internal=
* [[Go_Development_and_Execution_Environment#The_go_Tool|Go Development and Execution Environment]]
* [[Go_Operations|Go Operations]]
=Overview=
<code>go</code> is a command line tool with multiple uses: package manager, build tool and test driver. <code>go</code> manage packages in [[Go_Language_Modularization#Workspace|workspaces]], query metadata about packages, print documentation, build, format, download, test, etc.
=Shared Flags=
{{Internal|Go_Tool_Shared_Flags#Overview|Shared Flags}}
 
=Commands=
=Commands=
==<tt>build</tt>==
==Help==
The <code>build</code> command compiles and builds an executable.
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
cd ${PROJECT_DIR}
go help <command>
go build ./src/main/main.go
</syntaxhighlight>
</syntaxhighlight>
The command will create an executable named after the first source file, in this case <code>main</code>, and will place it by default in the <code>${PROJECT_DIR}</code> directory.
<syntaxhighlight lang='bash'>
go help build
</syntaxhighlight>
==<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>}}


==<tt>doc</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>==
The <code>doc</code> command prints documentation for a package.
{{Internal|go install#Overview|<tt>go install</tt>}}


==<tt>run</tt>==
==<tt>run</tt>==
The <code>run</code> command compiles the specified packages or files by delegating to <code>[[#build|go build]]</code> and then runs the executable. There must be a <code>main</code> for an executable to be generated.
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
cd $PROJECT_DIR
cd $PROJECT_DIR
go run ./src/main/main.go
go run ./src/main/main.go some-arg-1 some-arg-2
</syntaxhighlight>
The first argument that does not end in <code>.go</code> is assumed to be the beginning of the list of command line arguments of the executable.
 
<font color=darkkhaki>Is the above true though? The following command runs fine, where "example.com/experimental-go-module/cmd/gotest" is the package path of a "main" package inside of the "example.com/experimental-go-module" module. No argument ends in <code>.go</code>, yet it works:
<syntaxhighlight lang='bash'>
go run example.com/experimental-go-module/cmd/gotest
</syntaxhighlight>
</font>
 
The shared flags described here apply: {{Internal|Go_Tool_Shared_Flags#Overview|Shared Flags}}
 
==<tt>clean</tt>==
The shared flags described here apply: {{Internal|Go_Tool_Shared_Flags#Overview|Shared Flags}}
===<tt>-cache</tt>===
Clean the [[Go_Build_Cache#Overview|build cache]]:
<syntaxhighlight lang='go'>
go clean -cache
</syntaxhighlight>
===<tt>-fuzzcache</tt>===
Clean the [[Go_Build_Cache#Fuzz_Cache|fuzz cache]]:
<syntaxhighlight lang='go'>
go clean -fuzzcache
</syntaxhighlight>
</syntaxhighlight>
==<tt>link</tt>==
{{Internal|go tool link#Overview|<tt>go tool link</tt>}}
==<tt>doc</tt>==
{{Internal|Go_doc#Overview|<tt>go doc</tt>}}
==<span id='fmt'></span><tt>fmt</tt> (<tt>gofmt</tt>)==
{{Internal|Go_fmt#Overview|<tt>go fmt</tt>}}
==<span id='Problems_with_get'></span><tt>get</tt>==
{{Internal|Go get#Overview|<tt>go get</tt>}}
==<tt>list</tt>==
{{Internal|Go_list#Overview|<tt>go list</tt>}}
==<tt>test</tt>==
{{Internal|Go test Command|<tt>go test</code> Command}}
==<tt>env</tt>==
{{Internal|go env#Overview|<tt>go env</tt>}}
==<tt>mod</tt>==
{{Internal|go mod#Overview|<tt>go mod</tt>}}
==<tt>vet</tt>==
<font color=darkkhaki>
Deplete: [[Go Commands - vet|vet]]
</font>
==<tt>version</tt>==
<font color=darkkhaki>
Deplete [[Go Commands - version|version]]
</font>

Latest revision as of 08:07, 23 November 2024

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.

Is the above true though? The following command runs fine, where "example.com/experimental-go-module/cmd/gotest" is the package path of a "main" package inside of the "example.com/experimental-go-module" module. No argument ends in .go, yet it works:

go run example.com/experimental-go-module/cmd/gotest

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

link

go tool link

doc

go doc

fmt (gofmt)

go fmt

get

go get

list

go list

test

go test Command

env

go env

mod

go mod

vet

Deplete: vet

version

Deplete version