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:
bug
start a bug report
build
compile packages and dependencies
clean
remove object files and cached files
doc
show documentation for package or symbol
env
print Go environment information
fix
update packages to use new APIs
fmt
gofmt (reformat) package sources
generate
generate Go files by processing source
get
add dependencies to current module and install them
install
compile and install packages and dependencies
list
list packages or modules
mod
module maintenance
run
compile and run Go program
test
test packages
tool
run specified go tool
version
print Go version
vet
report likely mistakes in packages
Additional help topics:
buildconstraint
build constraints
buildmode
build modes
c
calling between Go and C
cache
build and test caching
environment
environment variables
filetype
file types
go.mod
the go.mod file
gopath
GOPATH environment variable
gopath-get
legacy GOPATH go get
goproxy
module proxy protocol
importpath
import path syntax
modules
modules, module versions, and more
module-get
module-aware go get
module-auth
module authentication using go.sum
packages
package lists and patterns
private
configuration for downloading non-public code
testflag
testing flags
testfunc
testing functions
vcs
controlling version control with GOVCS

Print Go version

Usage: 
  go version [-m] [-v] [file ...]

flags:
-v
The -v flag causes it to report unrecognized files.
-m
The -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:
-n
The -n flag prints commands that would be executed. The -x flag prints commands as they are executed.
-vettool=prog
The -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:
-json
The -json flag prints the environment in JSON format instead of as a shell script.
-u
The -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'.
-w
The -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:
-n
The -n flag prints commands that would be executed. The -x flag prints commands as they are executed.
-mod
The -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.
-v
The -v flag prints the names of packages and files as they are processed.
-n
The -n flag prints commands that would be executed.
-x
The -x flag prints commands as they are executed.

Related Tags

#golang# #go#