Go Tool: Difference between revisions
Line 68: | Line 68: | ||
==<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>== | ==<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>== | ||
{{Internal|go install#Overview|<tt>go install</tt>}} | {{Internal|go install#Overview|<tt>go install</tt>}} | ||
==<tt>run</tt>== | ==<tt>run</tt>== |
Revision as of 00:52, 3 October 2023
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 arguments, the tool creates an executable as described below. The following flags apply:
TO DEPLETE: Go_Commands_-_build
Build an Executable with go build
go build [options] <main-package-import-path>
Note that depending on whether the package is part of module or not, the main package import path may include the module path.
The executable is placed in the directory go build
is executed from.
The name of the executable is given by the last segment of the main package import path, and it can be configured with the -o
command line option. The argument of -o
can be the new name of the executable file, in which case the executable will be written in the current directory, or an absolute or relative path, in which case the executable, named after the last segment of the path, will be written at the given path.
Package without Module Support
In case of a package without module support, with the go
tool configured in GOPATH mode (GOPATH
configured to include the parent of the src
directory and GO111MODULE
set to "auto"), and with this directory layout:
. └─ src └─ a └─ b └─ c └─ main.go # package main
the executable is built with:
go build a/b/c
The executable will be named "c", based on the last segment of the package import path and will be place in the directory go build
was executed from. To change the name of the executable, use the -o
option:
go build -o blue a/b/c
This will create an executable named "blue".
Package within a Module
An a/b/c
main package with a similar layout, but this time included within an example.com
module
. ├─ a │ └─ b │ └─ c │ └─ main.go # package main └─ go.mod
can have its executable built with:
go build [-o blue] example.com/a/b/c
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.
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
TO DEPLETE
Deplete: