Go Tool
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.
Commands
Help
go help <command>
go help build
build
The build
command compiles one or more packages. If a main
package is among the argument, the tools builds an executable as described below. The following flags apply:
Build an Executable with go build
is among the compiled packages. The tool is sensitive to GOPATH mode. For more details, see:
TO DEPLETE: Go_Commands_-_build Also see:
Ensure that go tool is not in module-aware mode by setting GO111MODULE
to "auto" and set the correct GOPATH
value. Then execute go build
:
go build -o <command-name> <package-1-import-path> <package-2-import-path> ...
go build -o ./color-tool main # colors and misc/shapes is implied
go build
is capable of detecting dependencies, so not all package import paths should be specified in the command line. go build
will build an executable, by invoking the linker, if the package main
is among the compiled packages, otherwise it will just compile the packages and discard the result. For details on how the executable name is chosen, see the main
package.
install
go install
is similar to go build
except that is saves the compiled code as object files locally for each packages instead of discarding it.
The shared flags describe here apply:
If executed from a git workarea, there is underlying git interaction.
Build and Install an Executable with go install
The name of the executable is given by default by the name of the first source code file from the list of source files that make the main
package. This behavior can be changed with the -o
go
tool option.
The executable is placed in a location depending on the values of GOPATH
and GOBIN
environment variables. For more details see Publishing executable packages and Publishing executable modules.
The name of the executable will be given by:
The name can be changed by:
The executable will be placed in:
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:
clean
The shared flags described here apply:
-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:
list
The list
command lists all installed packages. The shared flags described here apply:
test
The test
command runs tests. The shared flags described here apply:
For more details, see:
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
Initialize a v0 or v1 module:
go mod init example.com/m
Initialize a v2 module:
go mod init example.com/m/v2
Inspect the code, look for module imports, download them and update go.mod
.
go mod tidy
TO DEPLETE
Deplete: