Go Tool: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 80: Line 80:
The <code>get</code> command downloads packages and installs them. The shared flags described here apply: {{Internal|Go_Tool_Shared_Flags#Overview|Shared Flags}}
The <code>get</code> command downloads packages and installs them. The shared flags described here apply: {{Internal|Go_Tool_Shared_Flags#Overview|Shared Flags}}
{{Internal|Go.mod#Adding_a_Dependency_to_a_Module|<tt>go.mod</tt> &#124; <tt>go get</tt>}}
{{Internal|Go.mod#Adding_a_Dependency_to_a_Module|<tt>go.mod</tt> &#124; <tt>go get</tt>}}
===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>==

Revision as of 20:15, 3 January 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

doc

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

go doc time
go doc time.Since

Deplete: doc

fmt (gofmt)

https://pkg.go.dev/cmd/gofmt

The fmt ("format") command formats source code files, by applying a predetermined layout to Go source code. It delegates to gofmt, and it is equivalent to:

gofmt -l -w <file>

One counterintuitive fact is that it uses the tab character for indentation. This is uncommon.

Rob Pike: "gofmt style is no one's favorite, yet gofmt is everyone's favorite".

get

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

Shared Flags
go.mod | go get

Options

-u

The -u flag instructs get to update modules providing dependencies of packages named on the command line to use newer minor or patch releases when available.

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

go env

get

The get command retrieve and update packages.

TODO Addison-Wesley The Go Programming Language Section 10.7.2

mod

go mod

vet

Deplete: vet

version

Deplete version