Go Tool: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(16 intermediate revisions by the same user not shown)
Line 50: Line 50:
go clean -fuzzcache
go clean -fuzzcache
</syntaxhighlight>
</syntaxhighlight>
==<tt>link</tt>==
{{Internal|go tool link#Overview|<tt>go tool link</tt>}}


==<tt>doc</tt>==
==<tt>doc</tt>==
The <code>doc</code> command prints documentation for a package or a package member:
{{Internal|Go_doc#Overview|<tt>go doc</tt>}}
<syntaxhighlight lang='go'>
go doc time
</syntaxhighlight>
<syntaxhighlight lang='go'>
go doc time.Since
</syntaxhighlight>
 
<font color=darkkhaki>
Deplete: [[Go Concepts - Documentation#Reading_Documentation|doc]]
</font>


==<span id='fmt'></span><tt>fmt</tt> (<tt>gofmt</tt>)==
==<span id='fmt'></span><tt>fmt</tt> (<tt>gofmt</tt>)==
{{External|https://pkg.go.dev/cmd/gofmt}}
{{Internal|Go_fmt#Overview|<tt>go fmt</tt>}}
 
The <code>fmt</code> ("format") command formats source code files, by applying a predetermined layout to Go source code. It delegates to <code>gofmt</code>, and it is equivalent to:


<syntaxhighlight lang='bash'>
==<span id='Problems_with_get'></span><tt>get</tt>==
gofmt -l -w <file>
{{Internal|Go get#Overview|<tt>go get</tt>}}
</syntaxhighlight>
 
One counterintuitive fact is that it uses the tab character for indentation. This is uncommon.
 
Rob Pike: "[https://www.youtube.com/watch?v=PAAkCSZUG1c&t=523s gofmt style is no one's favorite, yet gofmt is everyone's favorite]".
 
==<tt>get</tt>==
The <code>get</code> command downloads packages and installs them. It does that by connecting to the remote repository that maintains the package source code and downloading the source tree locally. <code>get</code> is the preferred way to update <code>go.mod</code> with a new dependency:
{{Internal|Go.mod#Adding_a_Dependency_to_a_Module|<tt>go.mod</tt> &#124; <tt>go get</tt>}}
Once <code>go.mod</code> has been updated by <code>go get</code>, the package's import pat can be used in the project's <code[[Go_Packages#Import_Statement|import]]</code> statements.
The shared flags described here apply: {{Internal|Go_Tool_Shared_Flags#Overview|Shared Flags}}
===Problems with <tt>get</tt>===
<font color=darkkhaki>The <code>import</code> statement drives <code>go get</code> but the <code>import</code> statement does not contain sufficient information to identify which revision of a package should be fetched any time <code>go get</code> is called. The possibility that <code>go get</code> can fetch a different version of code for any given package at any time makes supporting the Go tooling in any reproducible solution complicated and tedious.</font>
===Options===
====<tt>-u</tt>====
The <code>-u</code> flag instructs <code>get</code> to update modules providing dependencies of packages named on the command line to use newer minor or patch releases when available.


==<tt>list</tt>==
==<tt>list</tt>==
The <code>list</code> command lists all installed packages. The shared flags described here apply: {{Internal|Go_Tool_Shared_Flags#Overview|Shared Flags}}
{{Internal|Go_list#Overview|<tt>go list</tt>}}


==<tt>test</tt>==
==<tt>test</tt>==
The <code>test</code> command runs tests. The shared flags described here apply: {{Internal|Go_Tool_Shared_Flags#Overview|Shared Flags}}
{{Internal|Go test Command|<tt>go test</code> Command}}
For more details, see:
{{Internal|Go_Testing#Overview|Go Testing}}


==<tt>env</tt>==
==<tt>env</tt>==
{{Internal|go env#Overview|<tt>go env</tt>}}
{{Internal|go env#Overview|<tt>go env</tt>}}


==<tt>get</tt>==
The <code>get</code> command retrieve and update packages.
<font color=darkkhaki>TODO Addison-Wesley The Go Programming Language Section 10.7.2</font>
==<tt>mod</tt>==
==<tt>mod</tt>==
{{Internal|go mod#Overview|<tt>go mod</tt>}}
{{Internal|go mod#Overview|<tt>go mod</tt>}}

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