Go Tool

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

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

The get command downloads packages and installs them, possibly updating a local obsolete version. It does that by connecting to the remote repository that maintains the package source code and downloading the source tree locally. get is the preferred way to update go.mod with a new dependency:

go.mod | go get

Once go.mod has been updated by go get, the package's import pat can be used in the project's import statements.

go get is sensitive to the Git configuration present in ~/.gitconfig, especially repository configuration:

[url "git@github.example.com:some-repo"]
       insteadOf = https://github.example.com/some-repo

The shared flags described here apply:

Shared Flags

Problems with get

The import statement drives go get but the import statement does not contain sufficient information to identify which revision of a package should be fetched any time go get is called. The possibility that go get 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.

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.

-d

Stop after downloading the package, do not install the package.

-f

Only valid when -u is set, forces get -u not to verify that each package has been checked out from the source control repository implied by its import path. This can be useful if the source is a local fork of the original.

-fix

Run the fix tool on the downloaded packages before resolving dependencies or building the code.

-t

Also download the packages required to build the tests for the specified packages.

list

go list

test

go test Command

env

go env

mod

go mod

vet

Deplete: vet

version

Deplete version