How to use the go command line
Created
Modified
Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
Go install.
1. Extract the archive you downloaded into /usr/local, creating a Go tree in /usr/local/go.
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.16.5.linux-amd64.tar.gz
2. Add /usr/local/go/bin to the PATH environment variable.
export PATH=$PATH:/usr/local/go/bin
vi ~/.profile
source ~/.profile
3. Verify that you've installed Go by opening a command prompt and typing the following command:
go version
go version go1.16.5 darwin/amd64
Get started with Hello, World.
go mod init example.com/hello
go run .
go: creating new go.mod: module example.com/hello Hello, World!
Use "go help <command>" for more information about a command.
Help
go help build
go help run
usage: go run [build flags] [-exec xprog] package [arguments...] Run compiles and runs the named main Go package. ......
The go Help Commands and Options
Usage:
go <command> [arguments]
Commands:
bugstart a bug reportbuildcompile packages and dependenciescleanremove object files and cached filesdocshow documentation for package or symbolenvprint Go environment informationfixupdate packages to use new APIsfmtgofmt (reformat) package sourcesgenerategenerate Go files by processing sourcegetadd dependencies to current module and install theminstallcompile and install packages and dependencieslistlist packages or modulesmodmodule maintenanceruncompile and run Go programtesttest packagestoolrun specified go toolversionprint Go versionvetreport likely mistakes in packagesAdditional help topics:
buildconstraintbuild constraintsbuildmodebuild modesccalling between Go and Ccachebuild and test cachingenvironmentenvironment variablesfiletypefile typesgo.modthe go.mod filegopathGOPATH environment variablegopath-getlegacy GOPATH go getgoproxymodule proxy protocolimportpathimport path syntaxmodulesmodules, module versions, and moremodule-getmodule-aware go getmodule-authmodule authentication using go.sumpackagespackage lists and patternsprivateconfiguration for downloading non-public codetestflagtesting flagstestfunctesting functionsvcscontrolling version control with GOVCS
Print Go version
Usage:
go version [-m] [-v] [file ...]
flags:
-vThe -v flag causes it to report unrecognized files.-mThe -m flag causes go version to print each executable's embedded module version information, when available. In the output, the module information consists of multiple lines following the version line, each indented by a leading tab character.
Report likely mistakes in packages
Usage:
go vet [-n] [-x] [-vettool prog] [build flags] [vet flags] [packages]
flags:
-nThe -n flag prints commands that would be executed. The -x flag prints commands as they are executed.-vettool=progThe -vettool=prog flag selects a different analysis tool with alternative or additional checks. For example, the 'shadow' analyzer can be built and run using these commands:
Print Go environment information
Usage:
go env [-json] [-u] [-w] [var ...]
flags:
-jsonThe -json flag prints the environment in JSON format instead of as a shell script.-uThe -u flag requires one or more arguments and unsets the default setting for the named environment variables, if one has been set with 'go env -w'.-wThe -w flag requires one or more arguments of the form NAME=VALUE and changes the default settings of the named environment variables to the given values.
Update packages to use new APIs
Fix runs the Go fix command on the packages named by the import paths.
Usage:
go fix [packages]
Gofmt (reformat) package sources
Usage:
go fmt [-n] [-x] [packages]
flags:
-nThe -n flag prints commands that would be executed. The -x flag prints commands as they are executed.-modThe -mod flag's value sets which module download mode to use: readonly or vendor. See 'go help modules' for more.
Generate Go files by processing source
Generate runs commands described by directives within existing files. Those commands can run any process but the intent is to create or update Go source files.
Usage:
go generate [-run regexp] [-n] [-v] [-x] [build flags] [file.go... | packages]
flags:
-run=""if non-empty, specifies a regular expression to select directives whose full original source text (excluding any trailing spaces and final newline) matches the expression.-vThe -v flag prints the names of packages and files as they are processed.-nThe -n flag prints commands that would be executed.-xThe -x flag prints commands as they are executed.