Go Tool: Difference between revisions
Line 32: | Line 32: | ||
===Build and Install an Executable with <tt>go install</tt>=== | ===Build and Install an Executable with <tt>go install</tt>=== | ||
<syntaxhighlight lang='go'> | |||
</syntaxhighlight> | |||
<font color=darkkhaki>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 <code>main</code> package. This behavior can be changed with the <code>-o</code> [[Go_Tool#Overview|<code>go</code> tool]] option. | |||
The executable is placed in a location depending on the values of <code>GOPATH</code> and <code>GOBIN</code> environment variables. For more details see [[#Executables|Publishing executable packages]] and [[Go_Modules#Executables|Publishing executable modules]]. | |||
</font> | |||
The name of the executable will be given by: | |||
The name can be changed by: | |||
The executable will be placed in: | |||
==<tt>run</tt>== | ==<tt>run</tt>== |
Revision as of 19:29, 29 September 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 and builds the executable if the package main
is among the compiled packages. The tool is sensitive to GOPATH mode. For more details, see:
TO DEPLETE: Go_Commands_-_build
Also see:
Build an Executable with go build
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: