Go语言自带一套完整的命令行工具,你可以通过在命令行中执go
命令来查看它们:
Go is a tool for managing Go source code.
Go是一个管理Go源代码的工具。
Usage:
用法
go <command> [arguments]
go <命令> [参数]
The commands are:
命令如下:
bug start a bug report 开始报告一个bug
build compile packages and dependencies 编译包和依赖项
clean remove object files and cached files 删除目标文件和缓存文件
doc show documentation for package or symbol 显示包或符号的文档
env print Go environment information 打印Go环境信息
fix update packages to use new APIs 更新软件包以使用新的API
fmt gofmt (reformat) package sources gofmt(重新格式化)包源
generate generate Go files by processing source 通过处理源生成Go文件
get add dependencies to current module and install them 将依赖项添加到当前模块并安装它们
install compile and install packages and dependencies 编译和安装包和依赖项
list list packages or modules 列出包或模块
mod module maintenance 模块维护
work workspace maintenance 工作空间维护
run compile and run Go program 编译并运行Go程序
test test packages 测试软件包
tool run specified go tool 运行指定的go工具
version print Go version 打印Go版本号
vet report likely mistakes in packages 报告包中可能出现的错误
Use "go help <command>" for more information about a command.
使用“go help <命令>”获取有关命令的更多信息。
Additional help topics:
其他帮助主题:
buildconstraint build constraints 构建约束
buildmode build modes 构建模式
c calling between Go and C Go和C之间的调用
cache build and test caching 构建和测试缓存
environment environment variables 环境变量
filetype file types 文件类型
go.mod the go.mod file go.mod文件
gopath GOPATH environment variable GOPATH环境变量
gopath-get legacy GOPATH go get 遗留的GOPATH模式的go get命令
goproxy module proxy protocol 模块代理协议
importpath import path syntax 导入路径的语法
modules modules, module versions, and more 模块、模块版本等
module-get module-aware go get 模块模式的go get命令
module-auth module authentication using go.sum 使用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 使用GOVCS进行版本控制
Use "go help <topic>" for more information about that topic.
使用“go help <主题>”获取有关该主题的更多信息。
以上是Go 1.18版本的go
命令的输出。
我们使用这些工具开发Go语言项目,因此有必要熟识和掌握这些工具的使用。
go help 查看指定命令或主题的帮助文档
使用go help <命令>
或go help <主题>
查看这个命令或主题的帮助文档,例如使用go help build
查看go build命令详细信息。
go bug开始报告一个bug
$ go help bug
usage: go bug
用法:go bug
Bug opens the default browser and starts a new bug report.
Bug打开默认浏览器并开始一个新的bug报告。
The report includes useful system information.
该报告包括有用的系统信息。
go build编译包和依赖项
$ go help build
usage: go build [-o output] [build flags] [packages]
用法:go build [-o output] [build flags] [packages]
Build compiles the packages named by the import paths,
along with their dependencies, but it does not install the results.
Build编译由导入路径命名的包以及它们的依赖项,但它不会安装结果。
If the arguments to build are a list of .go files from a single directory,
build treats them as a list of source files specifying a single package.
如果构建参数是来自单个目录的.go文件的列表,则构建将它们视为指定的单个包的源文件列表。
When compiling packages, build ignores files that end in '_test.go'.
编译包时,build会忽略以'_test.go'结尾的文件。
When compiling a single main package, build writes
the resulting executable to an output file named after
the first source file ('go build ed.go rx.go' writes 'ed' or 'ed.exe')
or the source code directory ('go build unix/sam' writes 'sam' or 'sam.exe').
The '.exe' suffix is added when writing a Windows executable.
编译单个main包时,build将生成的可执行文件写入以第一个源文件命名的输出文件('go build ed.go rx.go'写入'ed'或'ed.exe')或源代码目录('go build unix/sam'写入'sam'或'sam.exe')。编写Windows可执行文件时会添加“.exe”后缀。
When compiling multiple packages or a single non-main package,
build compiles the packages but discards the resulting object,
serving only as a check that the packages can be built.
当编译多个包或单个非main包时,build编译包但丢弃生成的对象,仅用作检查包是否可以构建。
The -o flag forces build to write the resulting executable or object
to the named output file or directory, instead of the default behavior described
in the last two paragraphs. If the named output is an existing directory or
ends with a slash or backslash, then any resulting executables
will be written to that directory.
-o 标志强制构建将生成的可执行文件或对象写入指定的输出文件或目录,而不是最后两段中描述的默认行为。如果命名的输出是现有目录或以斜杠或反斜杠结尾,则任何生成的可执行文件都将写入该目录。
The -i flag installs the packages that are dependencies of the target.
-i 标志安装作为目标依赖项的包。
The -i flag is deprecated. Compiled packages are cached automatically.
-i 标志已弃用。编译的包会被自动缓存。
The build flags are shared by the build, clean, get, install, list, run,
and test commands:
构建标志由build,clean,get,install,list,run和test命令共享:
-a
force rebuilding of packages that are already up-to-date. 强制重建已经是最新版本的包。
-n
print the commands but do not run them.打印命令但不运行它们。
-p n
the number of programs, such as build commands or
test binaries, that can be run in parallel.
The default is GOMAXPROCS, normally the number of CPUs available.
可以并行运行的程序的数量,例如构建命令或测试二进制文件。默认值为GOMAXPROCS,通常是可用的CPU数量。
-race
enable data race detection.
Supported only on linux/amd64, freebsd/amd64, darwin/amd64, darwin/arm64, windows/amd64,
linux/ppc64le and linux/arm64 (only for 48-bit VMA).
启用数据竞争检测。
仅在linux/amd64、freebsd/amd64、darwin/amd64、darwin/arm64、windows/amd64、linux/ppc64le和linux/arm64操作系统和CPU架构上受支持(仅适用于48位VMA)。
-msan
enable interoperation with memory sanitizer.
Supported only on linux/amd64, linux/arm64
and only with Clang/LLVM as the host C compiler.
On linux/arm64, pie build mode will be used.
启用与内存清理程序的互操作。
仅在linux/amd64、linux/arm64和Clang/LLVM作为主机C编译器时受支持。
在linux/arm64上,将使用pie构建模式。
-asan
enable interoperation with address sanitizer.
Supported only on linux/arm64, linux/amd64.
启用与地址清理程序的互操作。
仅在linux/arm64、linux/amd64上受支持。
-v
print the names of packages as they are compiled. 在编译时打印包的名称。
-work
print the name of the temporary work directory and
do not delete it when exiting.
打印临时工作目录的名称,并在退出时不要删除它。
-x
print the commands. 打印命令。
-asmflags '[pattern=]arg list'
arguments to pass on each go tool asm invocation. 传递给每个go tool asm调用的参数。
-buildmode mode
build mode to use. See 'go help buildmode' for more. 要使用的构建模式。 有关更多信息,请参阅“go help buildmode”。
-buildvcs
Whether to stamp binaries with version control information
("true", "false", or "auto"). By default ("auto"), version control
information is stamped into a binary if the main package, the main module
containing it, and the current directory are all in the same repository.
Use -buildvcs=false to always omit version control information, or
-buildvcs=true to error out if version control information is available but
cannot be included due to a missing tool or ambiguous directory structure.
是否使用版本控制信息(“true”、“false”或“auto”)标记二进制文件。默认情况下(“auto”),如果main包、包含它的主模块和当前目录都在同一个存储库中,则版本控制信息被标记为二进制文件。
使用-buildvcs=false始终忽略版本控制信息,或-buildvcs=true如果版本控制信息可用但由于缺少工具或不明确的目录结构而无法包含,则输出错误信息。
-compiler name
name of compiler to use, as in runtime.Compiler (gccgo or gc). 要使用的编译器的名称,即runtime.Compiler(gccgo或gc)。
-gccgoflags '[pattern=]arg list'
arguments to pass on each gccgo compiler/linker invocation. 传递给每个gccgo编译器/链接器调用的参数。
-gcflags '[pattern=]arg list'
arguments to pass on each go tool compile invocation. 传递给每个go工具编译调用的参数。
-installsuffix suffix
a suffix to use in the name of the package installation directory,
in order to keep output separate from default builds.
If using the -race flag, the install suffix is automatically set to race
or, if set explicitly, has _race appended to it. Likewise for the -msan
and -asan flags. Using a -buildmode option that requires non-default compile
flags has a similar effect.
在包安装目录的名称中使用的后缀,以便将输出与默认构建分开。
如果使用-race标志,安装后缀会自动设置为race,或者,如果明确设置,则会附加_race。-msan和-asan标志也是如此。使用需要非默认编译标志的-buildmode选项具有类似的效果。
-ldflags '[pattern=]arg list'
arguments to pass on each go tool link invocation. 传递给每个go工具链接调用的参数。
-linkshared
build code that will be linked against shared libraries previously
created with -buildmode=shared.
构建代码将链接到以前使用-buildmode=shared创建的共享库。
-mod mode
module download mode to use: readonly, vendor, or mod.
By default, if a vendor directory is present and the go version in go.mod
is 1.14 or higher, the go command acts as if -mod=vendor were set.
Otherwise, the go command acts as if -mod=readonly were set.
See https://golang.org/ref/mod#build-commands for details.
要使用的模块下载模式:readonly、vendor或mod。
默认情况下,如果存在vendor目录并且go.mod中的go版本为1.14或更高版本,则go命令的行为就像设置了-mod=vendor一样。
否则,go命令的行为就像设置了-mod=readonly一样。
有关详细信息,请参阅https://golang.org/ref/mod#build-commands。
-modcacherw
leave newly-created directories in the module cache read-write
instead of making them read-only.
将新创建的目录保留在模块缓存中可读写,而不是使它们只读。
-modfile file
in module aware mode, read (and possibly write) an alternate go.mod
file instead of the one in the module root directory. A file named
"go.mod" must still be present in order to determine the module root
directory, but it is not accessed. When -modfile is specified, an
alternate go.sum file is also used: its path is derived from the
-modfile flag by trimming the ".mod" extension and appending ".sum".
在模块感知模式下,读取(并可能写入)另一个go.mod文件,而不是模块根目录中的那个go.mod文件。一个名为“go.mod”的文件必须仍然存在才能确定模块根目录,但它不会被访问。当指定-modfile时,还使用备用go.sum文件:它的路径是从-modfile标志通过剪去“.mod”扩展名并附加“.sum”扩展名而得出的。
-overlay file
read a JSON config file that provides an overlay for build operations.
The file is a JSON struct with a single field, named 'Replace', that
maps each disk file path (a string) to its backing file path, so that
a build will run as if the disk file path exists with the contents
given by the backing file paths, or as if the disk file path does not
exist if its backing file path is empty. Support for the -overlay flag
has some limitations: importantly, cgo files included from outside the
include path must be in the same directory as the Go package they are
included from, and overlays will not appear when binaries and tests are
run through go run and go test respectively.
读取一个为构建操作提供覆盖的JSON配置文件。
该文件是一个JSON结构,具有一个名为“Replace”的字段,它将每个磁盘文件路径(一个字符串)映射到其后备文件路径,以便构建将运行,就好像磁盘文件路径存在,其路径内容由后备文件路径给出,或者如果其后备文件路径为空,则好像磁盘文件路径不存在。对-overlay标志的支持有一些限制:重要的是,从包含路径之外包含的cgo文件必须与包含它们的Go包位于同一目录中,并且通过go run和go test分别运行二进制文件和测试时不会使用覆盖文件。
-pkgdir dir
install and load all packages from dir instead of the usual locations.
For example, when building with a non-standard configuration,
use -pkgdir to keep generated packages in a separate location.
从dir而不是通常的位置安装和加载所有包。
例如,使用非标准的配置进行构建时,使用-pkgdir将生成的包保存在单独的位置。
-tags tag,list
a comma-separated list of build tags to consider satisfied during the
build. For more information about build tags, see the description of
build constraints in the documentation for the go/build package.
(Earlier versions of Go used a space-separated list, and that form
is deprecated but still recognized.)
在构建过程中使用的构建标签的逗号分隔列表。有关构建标签的更多信息,请参阅go/build包文档中的构建约束描述。
(早期版本的Go使用空格分隔的列表,这种形式已被弃用但仍可识别。)
-trimpath
remove all file system paths from the resulting executable.
Instead of absolute file system paths, the recorded file names
will begin either a module path@version (when using modules),
or a plain import path (when using the standard library, or GOPATH).
从生成的可执行文件中删除所有文件系统路径。
记录的文件名不是绝对文件系统路径,而是以模块路径@版本号(使用模块时)或普通导入路径(使用标准库或GOPATH时)开头。
-toolexec 'cmd args'
a program to use to invoke toolchain programs like vet and asm.
For example, instead of running asm, the go command will run
'cmd args /path/to/asm <arguments for asm>'.
The TOOLEXEC_IMPORTPATH environment variable will be set,
matching 'go list -f {{.ImportPath}}' for the package being built.
一个用于调用vet和asm等工具链程序的程序。
例如,go命令不会运行asm,而是运行'cmd args /path/to/asm <arguments for asm>'。
将设置TOOLEXEC_IMPORTPATH环境变量,匹配正在构建的包的“go list -f {{.ImportPath}}”。
The -asmflags, -gccgoflags, -gcflags, and -ldflags flags accept a
space-separated list of arguments to pass to an underlying tool
during the build. To embed spaces in an element in the list, surround
it with either single or double quotes. The argument list may be
preceded by a package pattern and an equal sign, which restricts
the use of that argument list to the building of packages matching
that pattern (see 'go help packages' for a description of package
patterns). Without a pattern, the argument list applies only to the
packages named on the command line. The flags may be repeated
with different patterns in order to specify different arguments for
different sets of packages. If a package matches patterns given in
multiple flags, the latest match on the command line wins.
For example, 'go build -gcflags=-S fmt' prints the disassembly
only for package fmt, while 'go build -gcflags=all=-S fmt'
prints the disassembly for fmt and all its dependencies.
-asmflags、-gccgoflags、-gcflags和-ldflags标志接受以空格分隔的参数列表,以在构建期间传递给底层工具。要在列表中的元素中嵌入空格,请用单引号或双引号将其括起来。参数列表前面可能有一个包模式和一个等号,这限制了该参数列表的使用以构建与该模式匹配的包(有关包模式的描述,请参见“go help packages”)。如果没有模式,参数列表仅适用于命令行上给出名字的包。这些标志可以以不同的模式重复,以便为不同的包集指定不同的参数。如果一个包与多个标志中给出的模式匹配,则命令行上的最新匹配项获胜。
例如,'go build -gcflags=-S fmt'仅打印包fmt的反汇编,而'go build -gcflags=all=-S fmt'打印fmt及其所有依赖项的反汇编。
For more about specifying packages, see 'go help packages'.
For more about where packages and binaries are installed,
run 'go help gopath'.
For more about calling between Go and C/C++, run 'go help c'.
有关指定包的更多信息,请参阅“go help packages”。
有关软件包和二进制文件安装位置的更多信息,请运行“go help gopath”。
有关Go和C/C++之间调用的更多信息,请运行“go help c”。
Note: Build adheres to certain conventions such as those described
by 'go help gopath'. Not all projects can follow these conventions,
however. Installations that have their own conventions or that use
a separate software build system may choose to use lower-level
invocations such as 'go tool compile' and 'go tool link' to avoid
some of the overheads and design decisions of the build tool.
注意:构建遵循某些约定,例如“go help gopath”所描述的约定。然而,并非所有项目都遵循这些约定。有自己的约定或使用单独的软件构建系统的安装可以选择使用较低级别的调用,例如“go tool compile”和“go tool link”,以避免构建工具的一些开销和设计决策。
See also: go install, go get, go clean.
另请参阅:go install, go get, go clean。
go clean删除目标文件和缓存文件
$ go help clean
usage: go clean [clean flags] [build flags] [packages]
用法:go clean [clean flags] [build flags] [packages]
Clean removes object files from package source directories.
The go command builds most objects in a temporary directory,
so go clean is mainly concerned with object files left by other
tools or by manual invocations of go build.
Clean从包源目录中删除对象文件。
go命令在临时目录中构建大多数对象,因此go clean主要涉及其他工具或手动调用go build留下的对象文件。
If a package argument is given or the -i or -r flag is set,
clean removes the following files from each of the
source directories corresponding to the import paths:
如果给定包参数或设置了-i或-r标志,则clean会从与导入路径对应的每个源目录中删除以下文件:
_obj/ old object directory, left from Makefiles 旧对象目录,遗留自Makefiles
_test/ old test directory, left from Makefiles 旧的测试目录,遗留自Makefiles
_testmain.go old gotest file, left from Makefiles 旧的gotest文件,遗留自Makefiles
test.out old test log, left from Makefiles 旧的测试日志,遗留自Makefiles
build.out old test log, left from Makefiles 旧的测试日志,遗留自Makefiles
*.[568ao] object files, left from Makefiles 目标文件,遗留自Makefiles
DIR(.exe) from go build 来自go build
DIR.test(.exe) from go test -c 来自go test -c
MAINFILE(.exe) from go build MAINFILE.go 来自go build MAINFILE.go
*.so from SWIG 来自SWIG
In the list, DIR represents the final path element of the
directory, and MAINFILE is the base name of any Go source
file in the directory that is not included when building
the package.
在列表中,DIR表示目录的最终路径元素,而MAINFILE是该目录中构建包时未包含的任何Go源文件的基本名称。
The -i flag causes clean to remove the corresponding installed
archive or binary (what 'go install' would create).
-i标志让clean删除相应的已安装存档或二进制文件(这些是'go install' 会创建的)。
The -n flag causes clean to print the remove commands it would execute,
but not run them.
-n标志让clean打印它将执行的删除命令,但不运行它们。
The -r flag causes clean to be applied recursively to all the
dependencies of the packages named by the import paths.
-r标志让clean递归地应用于导入路径给出名字的包的所有依赖项。
The -x flag causes clean to print remove commands as it executes them.
-x标志让clean在执行删除命令时打印它们。
The -cache flag causes clean to remove the entire go build cache.
-cache标志让clean删除整个go build缓存。
The -testcache flag causes clean to expire all test results in the
go build cache.
-testcache标志会导致clean使go-build缓存中的所有测试结果过期。
The -modcache flag causes clean to remove the entire module
download cache, including unpacked source code of versioned
dependencies.
-modcache标志会让clean删除整个模块下载缓存,包括带版本号的依赖项的未解包的源代码。
The -fuzzcache flag causes clean to remove files stored in the Go build
cache for fuzz testing. The fuzzing engine caches files that expand
code coverage, so removing them may make fuzzing less effective until
new inputs are found that provide the same coverage. These files are
distinct from those stored in testdata directory; clean does not remove
those files.
-fuzzcache标志会导致clean删除Go构建缓存中存储的用于模糊测试的文件。模糊引擎缓存文件扩展代码覆盖率测试,因此在找到提供相同覆盖率的新输入之前,删除这些文件可能会降低模糊的有效性。这些文件不同于存储在testdata目录中的文件;clean不会删除这些文件。
For more about build flags, see 'go help build'.
有关构建标志的详细信息,请参阅“go help build”。
For more about specifying packages, see 'go help packages'.
有关指定包的详细信息,请参阅“go help packages”。
go doc显示包或符号(symbol)的文档
$ go help doc
usage: go doc [doc flags] [package|[package.]symbol[.methodOrField]]
用法:go doc [doc flags] [package|[package.]symbol[.methodOrField]]
Doc prints the documentation comments associated with the item identified by its
arguments (a package, const, func, type, var, method, or struct field)
followed by a one-line summary of each of the first-level items "under"
that item (package-level declarations for a package, methods for a type,
etc.).
Doc打印与其参数给出的条目相关的文档注释(包,常量,函数,类型,变量,方法或结构字段),然后是每个第一级条目(软件包的包级声明,类型的方法等)“下”的单行注释。
Doc accepts zero, one, or two arguments.
Doc接收0个,1个或2个参数。
Given no arguments, that is, when run as
如果没有给出参数,即执行
go doc
it prints the package documentation for the package in the current directory.
If the package is a command (package main), the exported symbols of the package
are elided from the presentation unless the -cmd flag is provided.
它打印输出当前目录中的软件包的包文档。
如果当前目录中的软件包是命令(main包),则除非提供-cmd标志,否则包的导出符号将从演示中省略。
When run with one argument, the argument is treated as a Go-syntax-like
representation of the item to be documented. What the argument selects depends
on what is installed in GOROOT and GOPATH, as well as the form of the argument,
which is schematically one of these:
当使用一个参数运行时,该参数被视为要输出文档的条目的类似于Go语法的表示。参数选择的内容取决于GOROOT和GOPATH中安装的内容以及参数的形式,是以下形式之一:
go doc <pkg>
go doc <sym>[.<methodOrField>]
go doc [<pkg>.]<sym>[.<methodOrField>]
go doc [<pkg>.][<sym>.]<methodOrField>
The first item in this list matched by the argument is the one whose documentation
is printed. (See the examples below.) However, if the argument starts with a capital
letter it is assumed to identify a symbol or method in the current directory.
该参数匹配的此列表中的第一个条目是打印文档的内容。(请参见下面的示例。)但是,如果参数以大写字母开头,则假定该参数是当前目录中的符号或方法。
For packages, the order of scanning is determined lexically in breadth-first order.
That is, the package presented is the one that matches the search and is nearest
the root and lexically first at its level of the hierarchy. The GOROOT tree is
always scanned in its entirety before GOPATH.
对于包,扫描顺序在词法上以广度优先确定。
也就是说,呈现的包是与搜索匹配的包,并且在搜索的层次结构的级别上最接近树根,词法上的第一个匹配。GOROOT树总是在GOPATH树之前全部扫描。
If there is no package specified or matched, the package in the current
directory is selected, so "go doc Foo" shows the documentation for symbol Foo in
the current package.
如果没有指定或匹配的软件包,则选择当前目录中的软件包,因此“go doc Foo”显示了当前软件包中符号FOO的文档。
The package path must be either a qualified path or a proper suffix of a
path. The go tool's usual package mechanism does not apply: package path
elements like . and ... are not implemented by go doc.
包装路径必须是合格的路径或路径的适当后缀。GO工具的常用的包机制不适用:包装路径元素像.和...未由go doc实现。
When run with two arguments, the first is a package path (full path or suffix),
and the second is a symbol, or symbol with method or struct field:
当给出两个参数运行时,第一个是一个包路径(完整路径或后缀),第二个是一个符号或带有方法或结构字段的符号:
go doc <pkg> <sym>[.<methodOrField>]
In all forms, when matching symbols, lower-case letters in the argument match
either case but upper-case letters match exactly. This means that there may be
multiple matches of a lower-case argument in a package if different symbols have
different cases. If this occurs, documentation for all matches is printed.
在所有形式中,当匹配符号时,参数中的小写字母匹配任何一种大小写,但大写字母进行精确匹配。这意味着,如果不同的符号具有不同的大小写,则包中可能存在多个小写参数的匹配项。如果发生这种情况,将打印所有匹配项的文档。
Examples:
示例:
go doc
Show documentation for current package. 显示当前包的文档。
go doc Foo
Show documentation for Foo in the current package.
(Foo starts with a capital letter so it cannot match
a package path.)
在当前包中显示Foo的文档。
(Foo以大写字母开头,因此无法匹配包路径。)
go doc encoding/json
Show documentation for the encoding/json package. 显示encoding/json包的文档。
go doc json
Shorthand for encoding/json. encoding/json的速记形式。
go doc json.Number (or go doc json.number)
Show documentation and method summary for json.Number. 显示json.Number的文档和方法摘要。
go doc json.Number.Int64 (or go doc json.number.int64)
Show documentation for json.Number's Int64 method. 显示json.Number的Int64方法的文档。
go doc cmd/doc
Show package docs for the doc command. 显示doc命令的包文档。
go doc -cmd cmd/doc
Show package docs and exported symbols within the doc command. 在doc命令中显示包文档和导出的符号。
go doc template.new
Show documentation for html/template's New function.
(html/template is lexically before text/template)
显示html/template的New函数的文档。
(html/template在词法上先于text/template)
go doc text/template.new # One argument 一个参数
Show documentation for text/template's New function. 显示text/template的New函数的文档。
go doc text/template new # Two arguments 两个参数
Show documentation for text/template's New function. 显示text/template的New函数的文档。
At least in the current tree, these invocations all print the
documentation for json.Decoder's Decode method:
至少在当前树中,这些调用都打印了json.Decoder的Decode方法的文档:
go doc json.Decoder.Decode
go doc json.decoder.decode
go doc json.decode
cd go/src/encoding/json; go doc decode
Flags:
标志:
-all
Show all the documentation for the package. 显示包的所有文档。
-c
Respect case when matching symbols. 匹配时区分大小写。
-cmd
Treat a command (package main) like a regular package.
Otherwise package main's exported symbols are hidden
when showing the package's top-level documentation.
将命令(main包)视为常规包。
否则,当显示包的顶级文档时,main包的导出符号将被隐藏。
-short
One-line representation for each symbol. 每个符号一行显示。
-src
Show the full source code for the symbol. This will
display the full Go source of its declaration and
definition, such as a function definition (including
the body), type declaration or enclosing const
block. The output may therefore include unexported
details.
显示符号的完整源代码。这将显示其声明和定义的完整Go源代码,例如函数定义(包括主体)、类型声明或封闭的const块。因此,输出可能包括没有导出的详细信息。
-u
Show documentation for unexported as well as exported
symbols, methods, and fields.
显示未导出和导出的符号、方法和字段的文档。
go env打印Go环境信息
$ go help env
usage: go env [-json] [-u] [-w] [var ...]
用法:go env [-json] [-u] [-w] [var ...]
Env prints Go environment information.
env打印输出Go环境信息。
By default env prints information as a shell script
(on Windows, a batch file). If one or more variable
names is given as arguments, env prints the value of
each named variable on its own line.
默认情况下,env作为shell脚本打印输出信息(在Windows上为批处理文件)。如果一个或多个变量名称作为参数给出,env将每个给出名字的变量的值在对应的行上打印输出。
The -json flag prints the environment in JSON format
instead of as a shell script.
-json标志以JSON格式而不是作为shell脚本打印环境。
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'.
-u标志需要一个或多个参数,并取消设置给出名字的环境变量的默认设置(如果已使用“go env -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.
-w标志需要一个或多个NAME=VALUE形式的参数,并将命名环境变量的默认设置更改为给定值。
For more about environment variables, see 'go help environment'.
有关环境变量的更多信息,请参阅“go help environment”。
go fix更新软件包以使用新的API
$ go help fix
usage: go fix [-fix list] [packages]
用法:go fix [-fix list] [packages]
Fix runs the Go fix command on the packages named by the import paths.
fix在由导入路径给出名字的包上运行Go的fix命令。
The -fix flag sets a comma-separated list of fixes to run.
The default is all known fixes.
(Its value is passed to 'go tool fix -r'.)
-fix标志设置以逗号分隔的要运行的修复列表。
默认是所有已知的修复。
(它的值被传递给'go tool fix -r'。)
For more about fix, see 'go doc cmd/fix'.
For more about specifying packages, see 'go help packages'.
有关修复的更多信息,请参阅“go doc cmd/fix”。
有关指定包的更多信息,请参阅“go help packages”。
To run fix with other options, run 'go tool fix'.
要使用其他选项运行修复,请运行“go tool fix”。
See also: go fmt, go vet.
另请参阅:go fmt,go vet。
go fmt重新格式化包源
$ go help fmt
usage: go fmt [-n] [-x] [packages]
用法:go fmt [-n] [-x] [packages]
Fmt runs the command 'gofmt -l -w' on the packages named
by the import paths. It prints the names of the files that are modified.
fmt在由导入路径给出名字的包上运行命令“gofmt -l -w”。它会打印输出被修改的文件的名称。
For more about gofmt, see 'go doc cmd/gofmt'.
For more about specifying packages, see 'go help packages'.
有关gofmt的更多信息,请参阅“go doc cmd/gofmt”。
有关指定包的更多信息,请参阅“go help packages”。
The -n flag prints commands that would be executed.
The -x flag prints commands as they are executed.
-n标志打印将要执行的命令。
-x标志在执行命令时打印命令。
The -mod flag's value sets which module download mode
to use: readonly or vendor. See 'go help modules' for more.
-mod标志的值设置要使用的模块下载模式:只读或vendor模式。有关更多信息,请参阅“go help modules”。
To run gofmt with specific options, run gofmt itself.
要使用特定选项运行gofmt,请运行gofmt本身。
See also: go fix, go vet.
另请参阅:go fix, go vet。
go generate通过处理源文件生成Go文件
$ go help generate
usage: go generate [-run regexp] [-n] [-v] [-x] [build flags] [file.go... | packages]
用法:go generate [-run regexp] [-n] [-v] [-x] [build flags] [file.go... | packages]
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.
generate运行现有文件中的指令描述的命令。这些命令可以运行任何进程,但目的是创建或更新Go源文件。
Go generate is never run automatically by go build, go test,
and so on. It must be run explicitly.
go generate永远不会被go build、go test等自动运行。它必须显式运行。
Go generate scans the file for directives, which are lines of
the form,
go generate扫描文件中的指令,这些指令是以下形式的行,
//go:generate command argument... go:生成命令参数
(note: no leading spaces and no space in "//go") where command
is the generator to be run, corresponding to an executable file
that can be run locally. It must either be in the shell path
(gofmt), a fully qualified path (/usr/you/bin/mytool), or a
command alias, described below.
(注意:没有前导空格并且“//go”中也没有空格),其中command是要运行的生成器,对应于可以在本地运行的可执行文件。它必须位于shell路径(gofmt)、完全限定路径(/usr/you/bin/mytool)或命令别名中,如下所述。
Note that go generate does not parse the file, so lines that look
like directives in comments or multiline strings will be treated
as directives.
请注意,go generate不会解析文件,因此在注释或多行字符串中看起来像指令的行将被视为指令。
The arguments to the directive are space-separated tokens or
double-quoted strings passed to the generator as individual
arguments when it is run.
指令的参数是空格分隔的标记或双引号包起来的字符串,在运行时作为单个参数传递给生成器。
Quoted strings use Go syntax and are evaluated before execution; a
quoted string appears as a single argument to the generator.
引号包起来的字符串使用Go语法,并在执行前进行评估;引号包起来的字符串作为生成器的单个参数。
To convey to humans and machine tools that code is generated,
generated source should have a line that matches the following
regular expression (in Go syntax):
为了向人和机器传达生成的代码,生成的源代码应该有一行匹配以下正则表达式(在Go语法中):
^// Code generated .* DO NOT EDIT\.$
This line must appear before the first non-comment, non-blank
text in the file.
此行必须出现在文件中第一个非注释、非空白文本之前。
Go generate sets several variables when it runs the generator:
Go generate在运行生成器时设置了几个变量:
$GOARCH
The execution architecture (arm, amd64, etc.) 执行环境的架构(arm、amd64 等。)
$GOOS
The execution operating system (linux, windows, etc.) 执行环境的操作系统(linux、windows等。)
$GOFILE
The base name of the file. 文件的基本名称。
$GOLINE
The line number of the directive in the source file. 源文件中指令所在的行号。
$GOPACKAGE
The name of the package of the file containing the directive. 包含指令的文件的包的名字。
$DOLLAR
A dollar sign. 一个美元符号。
Other than variable substitution and quoted-string evaluation, no
special processing such as "globbing" is performed on the command
line.
除了变量替换和带引号的字符串求值外,在命令行上不执行诸如“globbing”之类的特殊处理。
As a last step before running the command, any invocations of any
environment variables with alphanumeric names, such as $GOFILE or
$HOME, are expanded throughout the command line. The syntax for
variable expansion is $NAME on all operating systems. Due to the
order of evaluation, variables are expanded even inside quoted
strings. If the variable NAME is not set, $NAME expands to the
empty string.
作为运行命令之前的最后一步,对任何具有字母数字名称的环境变量(例如$GOFILE或$HOME)的任何调用都会在整个命令行中展开。变量扩展的语法在所有操作系统上都是$NAME。由于求值的顺序,变量甚至在带引号的字符串中也会被扩展。如果未设置变量NAME,则$NAME将扩展为空字符串。
A directive of the form,
一个以下形式的指令,
//go:generate -command xxx args...
specifies, for the remainder of this source file only, that the
string xxx represents the command identified by the arguments. This
can be used to create aliases or to handle multiword generators.
For example,
仅针对此源文件的其余部分,指定的字符串xxx表示由参数标识的命令。这可用于创建别名或处理多字生成器。
例如,
//go:generate -command foo go tool foo
specifies that the command "foo" represents the generator
"go tool foo".
指定"foo"命令代表"go tool foo"生成器。
Generate processes packages in the order given on the command line,
one at a time. If the command line lists .go files from a single directory,
they are treated as a single package. Within a package, generate processes the
source files in a package in file name order, one at a time. Within
a source file, generate runs generators in the order they appear
in the file, one at a time. The go generate tool also sets the build
tag "generate" so that files may be examined by go generate but ignored
during build.
按照命令行上给定的顺序生成进程包,一次一个。如果命令行列出了单个目录中的.go文件,它们将被视为单个包。在包中,generate按文件名顺序处理包中的源文件,一次一个。在源文件中,generate按照它们在文件中出现的顺序运行生成器,一次一个。go generate工具还设置了构建标签“generate”,以便go generate可以检查文件,但在构建过程中会忽略这些文件。
For packages with invalid code, generate processes only source files with a
valid package clause.
对于具有无效代码的包,仅生成具有有效包子句的源文件。
If any generator returns an error exit status, "go generate" skips
all further processing for that package.
如果任何生成器返回错误退出状态,“go generate”会跳过该包的所有进一步的处理。
The generator is run in the package's source directory.
生成器在包的源目录中运行。
Go generate accepts one specific flag:
go generate接受一个特定的标志:
-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.
如果非空,则指定一个正则表达式以选择其完整的原始源文本(不包括任何尾随空格和最后的换行符)与该表达式匹配的指令。
It also accepts the standard build flags including -v, -n, and -x.
The -v flag prints the names of packages and files as they are
processed.
The -n flag prints commands that would be executed.
The -x flag prints commands as they are executed.
它还接受标准构建标志,包括-v、-n和-x。
-v标志在处理包和文件时打印它们的名称。
-n标志打印将要执行的命令。
-x标志在执行命令时打印命令。
For more about build flags, see 'go help build'.
有关构建标志的更多信息,请参阅“go help build”。
For more about specifying packages, see 'go help packages'.
有关指定包的更多信息,请参阅“go help packages”。
go get将依赖项添加到当前模块并安装它们
$ go help get
usage: go get [-t] [-u] [-v] [build flags] [packages]
用法:go get [-t] [-u] [-v] [build flags] [packages]
Get resolves its command-line arguments to packages at specific module versions,
updates go.mod to require those versions, and downloads source code into the
module cache.
get将其命令行参数解析为特定模块版本的包,更新go.mod以需求这些版本,并将源代码下载到模块缓存中。
To add a dependency for a package or upgrade it to its latest version:
要为软件包添加依赖项或将其升级到最新版本:
go get example.com/pkg
To upgrade or downgrade a package to a specific version:
要将软件包升级或降级到特定版本:
go get example.com/[email protected]
To remove a dependency on a module and downgrade modules that require it:
删除对某个模块的依赖并降级需求它的模块:
go get example.com/mod@none
See https://golang.org/ref/mod#go-get for details.
有关详细信息,请参阅https://golang.org/ref/mod#go-get。
In earlier versions of Go, 'go get' was used to build and install packages.
Now, 'go get' is dedicated to adjusting dependencies in go.mod. 'go install'
may be used to build and install commands instead. When a version is specified,
'go install' runs in module-aware mode and ignores the go.mod file in the
current directory. For example:
在早期版本的Go中,“go get”用于构建和安装包。现在,'go get'专门用于调整go.mod中的依赖关系。'go install'可用于构建和安装命令。当指定版本时,“go install”以模块感知模式运行,并忽略当前目录中的go.mod 文件。例如:
go install example.com/[email protected]
go install example.com/pkg@latest
See 'go help install' or https://golang.org/ref/mod#go-install for details.
有关详细信息,请参阅“go help install”或https://golang.org/ref/mod#go-install。
'go get' accepts the following flags.
'go get'接受以下标志。
The -t flag instructs get to consider modules needed to build tests of
packages specified on the command line.
-t标志指示get考虑构建命令行上指定的包的测试所需的模块。
The -u flag instructs get to update modules providing dependencies
of packages named on the command line to use newer minor or patch
releases when available.
-u标志指示get更新提供包的依赖项的模块,以便在可用时使用更新次版本号或补丁版本号,这些模块在命令行上给出名字。
The -u=patch flag (not -u patch) also instructs get to update dependencies,
but changes the default to select patch releases.
-u=patch标志(不是-u patch)还指示get更新依赖项,但将默认值更改为选择补丁版本号。
When the -t and -u flags are used together, get will update
test dependencies as well.
当-t和-u标志一起使用时,get也会更新测试所需的依赖项。
The -x flag prints commands as they are executed. This is useful for
debugging version control commands when a module is downloaded directly
from a repository.
-x标志在执行命令时打印命令。当直接从存储库下载模块时,这对于调试版本控制命令很有用。
For more about modules, see https://golang.org/ref/mod.
有关模块的更多信息,请参阅https://golang.org/ref/mod。
For more about specifying packages, see 'go help packages'.
有关指定包的更多信息,请参阅“go help packages”。
This text describes the behavior of get using modules to manage source
code and dependencies. If instead the go command is running in GOPATH
mode, the details of get's flags and effects change, as does 'go help get'.
See 'go help gopath-get'.
本文描述了get使用模块来管理源代码和依赖项的行为。如果go命令在GOPATH模式下运行,则get的标志和效果的详细信息会发生变化,“go help get”也是如此。请参阅“go help gopath-get”。
See also: go build, go install, go clean, go mod.
另请参阅:go build, go install, go clean, go mod。
go install编译并安装包和依赖项
$ go help install
usage: go install [build flags] [packages]
用法:go install [build flags] [packages]
Install compiles and installs the packages named by the import paths.
install编译并安装由导入路径给出名字的包。
Executables are installed in the directory named by the GOBIN environment
variable, which defaults to $GOPATH/bin or $HOME/go/bin if the GOPATH
environment variable is not set. Executables in $GOROOT
are installed in $GOROOT/bin or $GOTOOLDIR instead of $GOBIN.
可执行文件安装在由GOBIN环境变量命名的目录中,默认为$GOPATH/bin,如果未设置GOPATH环境变量,则安装在$HOME/go/bin。$GOROOT中的可执行文件安装在$GOROOT/bin或$GOTOOLDIR而不是$GOBIN中。
If the arguments have version suffixes (like @latest or @v1.0.0), "go install"
builds packages in module-aware mode, ignoring the go.mod file in the current
directory or any parent directory, if there is one. This is useful for
installing executables without affecting the dependencies of the main module.
To eliminate ambiguity about which module versions are used in the build, the
arguments must satisfy the following constraints:
如果参数有版本号后缀(例如@latest或@v1.0.0),“go install”以模块感知模式构建包,忽略当前目录或任何父目录中的go.mod文件(如果有的话)。这对于在不影响主模块依赖关系的情况下安装可执行文件很有用。为了消除构建中使用的模块版本的歧义,参数必须满足以下约束:
- Arguments must be package paths or package patterns (with "..." wildcards).
They must not be standard packages (like fmt), meta-patterns (std, cmd,
all), or relative or absolute file paths.
参数必须是包路径或包模式(带有“...”通配符)。它们不能是标准包(例如fmt)、元模式(std、cmd、all)或相对或绝对文件路径。
- All arguments must have the same version suffix. Different queries are not
allowed, even if they refer to the same version.
所有参数必须具有相同的版本号后缀。不允许不同的查询,即使它们引用相同的版本。
- All arguments must refer to packages in the same module at the same version. 所有参数必须引用相同版本的相同模块中的包。
- Package path arguments must refer to main packages. Pattern arguments
will only match main packages.
包路径参数必须引用主包。模式参数只会匹配主包。
- No module is considered the "main" module. If the module containing
packages named on the command line has a go.mod file, it must not contain
directives (replace and exclude) that would cause it to be interpreted
differently than if it were the main module. The module must not require
a higher version of itself.
没有模块被视为“主”模块。如果包含在命令行中给出名字的包的模块有一个go.mod文件,则它不能包含replace和exclude指令,因为这两个指令会导致对它的解释与当它作为主模块时对它的解释不同。该模块不得需求其自身的更高版本。
- Vendor directories are not used in any module. (Vendor directories are not
included in the module zip files downloaded by 'go install'.)
Vendor目录不会在任何模块中使用。(Vendor目录不包含在“go install”下载的模块zip文件中。)
If the arguments don't have version suffixes, "go install" may run in
module-aware mode or GOPATH mode, depending on the GO111MODULE environment
variable and the presence of a go.mod file. See 'go help modules' for details.
If module-aware mode is enabled, "go install" runs in the context of the main
module.
如果参数没有版本号后缀,“go install”可能会在模块感知模式或GOPATH模式下运行,具体取决于GO111MODULE环境变量和go.mod文件的存在。有关详细信息,请参阅“go help modules”。如果启用了模块感知模式,“go install”将在主模块的上下文中运行。
When module-aware mode is disabled, other packages are installed in the
directory $GOPATH/pkg/$GOOS_$GOARCH. When module-aware mode is enabled,
other packages are built and cached but not installed.
当禁用模块感知模式时,其他包将安装在目录$GOPATH/pkg/$GOOS_$GOARCH中。当启用模块感知模式时,会生成和缓存其他包,但不会安装。
The -i flag installs the dependencies of the named packages as well.
The -i flag is deprecated. Compiled packages are cached automatically.
-i标志还安装命名包的依赖项。
-i标志已弃用。编译的包将自动缓存。
For more about the build flags, see 'go help build'.
For more about specifying packages, see 'go help packages'.
有关构建标志的详细信息,请参阅“go help build”。
有关指定包的详细信息,请参阅“go help packages”。
See also: go build, go get, go clean.
另请参阅:go build,go get,go clean。
go list列出当前全部安装的模块或包
$ go help list
usage: go list [-f format] [-json] [-m] [list flags] [build flags] [packages]
用法:go list [-f format] [-json] [-m] [list flags] [build flags] [packages]
List lists the named packages, one per line.
The most commonly-used flags are -f and -json, which control the form
of the output printed for each package. Other list flags, documented below,
control more specific details.
list列出命名的包,每行一个。
最常用的标志是-f和-json,它们控制为每个包打印输出的格式。下面记录的其他list标志控制更具体的细节。
The default output shows the package import path:
默认输出显示包的导入路径:
bytes
encoding/json
github.com/gorilla/mux
golang.org/x/net/html
The -f flag specifies an alternate format for the list, using the
syntax of package template. The default output is equivalent
to -f '{{.ImportPath}}'. The struct being passed to the template is:
-f标志使用包模板的语法指定list的替代格式。默认输出格式等效于-f '{{.ImportPath}}'。传递给模板的结构是:
type Package struct {
Dir string // directory containing package sources 包含包源代码的目录
ImportPath string // import path of package in dir 包在dir里的导入路径
ImportComment string // path in import comment on package statement 包语句的导入注释中的路径
Name string // package name 包名
Doc string // package documentation string 包文档字符串
Target string // install path 安装路径
Shlib string // the shared library that contains this package (only set when -linkshared) 包含此包的共享库(仅在-linkshared时设置)
Goroot bool // is this package in the Go root? 此包是否在Go安装目录里?
Standard bool // is this package part of the standard Go library? 此包是Go标准库的一部分吗?
Stale bool // would 'go install' do anything for this package? 'go install'是否会对此包做一些事情?
StaleReason string // explanation for Stale==true 对Stale==true的解释
Root string // Go root or Go path dir containing this package 包含此包的Go root或Go path目录
ConflictDir string // this directory shadows Dir in $GOPATH 此目录隐藏了$GOPATH中的目录
BinaryOnly bool // binary-only package (no longer supported) 只是二进制包(已经不再支持)
ForTest string // package is only for use in named test 只用于测试的包
Export string // file containing export data (when using -export) 包含导出数据的文件(使用-export时)
BuildID string // build ID of the compiled package (when using -export) 编译包的构建ID(使用-export时)
Module *Module // info about package's containing module, if any (can be nil) 关于包的包含模块的信息,如果有的话(可以是nil)
Match []string // command-line patterns matching this package 匹配此包的命令行模式
DepOnly bool // package is only a dependency, not explicitly listed 此包只是一个依赖项,没有显式地被列出
// Source files 源文件
GoFiles []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles) .go源文件(不包括CgoFiles、TestGoFiles、XTestGoFiles)
CgoFiles []string // .go source files that import "C" 导入“C”的.go源文件
CompiledGoFiles []string // .go files presented to compiler (when using -compiled) 呈现给编译器的.go文件(使用-compiled时)
IgnoredGoFiles []string // .go source files ignored due to build constraints 因构建约束而被忽略的.go源文件
IgnoredOtherFiles []string // non-.go source files ignored due to build constraints 由于构建约束而被忽略的非.go源文件
CFiles []string // .c source files .c源文件
CXXFiles []string // .cc, .cxx and .cpp source files .cc,.cxx和.cpp源文件
MFiles []string // .m source files .m源文件
HFiles []string // .h, .hh, .hpp and .hxx source files .h,.hh,.hpp和.hxx源文件
FFiles []string // .f, .F, .for and .f90 Fortran source files .f,.F,.for和.f90 Fortran源文件
SFiles []string // .s source files .s源文件
SwigFiles []string // .swig files .swig文件
SwigCXXFiles []string // .swigcxx files .swigcxx文件
SysoFiles []string // .syso object files to add to archive 要添加到文档的.syso目标文件
TestGoFiles []string // _test.go files in package 包里的_test.go文件
XTestGoFiles []string // _test.go files outside package 包外面的_test.go文件
// Embedded files 内嵌的文件
EmbedPatterns []string // //go:embed patterns go:内嵌模式
EmbedFiles []string // files matched by EmbedPatterns 匹配EmbedPatterns的文件
TestEmbedPatterns []string // //go:embed patterns in TestGoFiles go:在TestGoFiles里内嵌的模式
TestEmbedFiles []string // files matched by TestEmbedPatterns 匹配TestEmbedPatterns的文件
XTestEmbedPatterns []string // //go:embed patterns in XTestGoFiles go:在XTestGoFiles里内嵌的模式
XTestEmbedFiles []string // files matched by XTestEmbedPatterns 匹配XTestEmbedPatterns的文件
// Cgo directives Cgo指令
CgoCFLAGS []string // cgo: flags for C compiler cgo:C编译器cgo的标志
CgoCPPFLAGS []string // cgo: flags for C preprocessor cgo:C预处理器的标志
CgoCXXFLAGS []string // cgo: flags for C++ compiler cgo:C++编译器的标志
CgoFFLAGS []string // cgo: flags for Fortran compiler cgo:Fortran编译器的标志
CgoLDFLAGS []string // cgo: flags for linker cgo:连接器的标志
CgoPkgConfig []string // cgo: pkg-config names cgo:pkg-config的名字
// Dependency information 依赖信息
Imports []string // import paths used by this package 此包使用的导入路径
ImportMap map[string]string // map from source import to ImportPath (identity entries omitted) 从源导入映射到 ImportPath(省略标识条目)
Deps []string // all (recursively) imported dependencies 所有(递归地)导入的依赖项
TestImports []string // imports from TestGoFiles 从TestGoFiles的导入
XTestImports []string // imports from XTestGoFiles 从XTestGoFiles的导入
// Error information 错误信息
Incomplete bool // this package or a dependency has an error 次包或一个依赖项有一个错误
Error *PackageError // error loading package 加载包时出错
DepsErrors []*PackageError // errors loading dependencies 加载依赖项时出错
}
Packages stored in vendor directories report an ImportPath that includes the
path to the vendor directory (for example, "d/vendor/p" instead of "p"),
so that the ImportPath uniquely identifies a given copy of a package.
The Imports, Deps, TestImports, and XTestImports lists also contain these
expanded import paths. See golang.org/s/go15vendor for more about vendoring.
存储在vendor目录中的软件包报告一个ImportPath,其中包含vendor目录的路径(例如,“d/ivendor/p”而不是“p”),以便ImportPath唯一标识软件包的给定副本。
Imports、Deps、TestImports和XTestImports列表也包含这些扩展的导入路径。见golang.org/s/go15vendor了解更多关于vendor的信息。
The error information, if any, is
错误信息(如果有的话)是
type PackageError struct {
ImportStack []string // shortest path from package named on command line to this one 在命令行给出名字的包到这个包的最短路径
Pos string // position of error (if present, file:line:col) 错误位置(如果存在的话,文件名:行号:列号)
Err string // the error itself 错误本身
}
The module information is a Module struct, defined in the discussion
of list -m below.
模块信息是一个Module结构体,在下面list -m的讨论中定义。
The template function "join" calls strings.Join.
模板函数“join”调用strings.Join。
The template function "context" returns the build context, defined as:
模板函数“context”返回构建上下文,定义为:
type Context struct {
GOARCH string // target architecture 目标架构
GOOS string // target operating system 目标操作系统
GOROOT string // Go root Go语言安装路径
GOPATH string // Go path Go语言包的查找路径
CgoEnabled bool // whether cgo can be used 是否可以使用cgo
UseAllFiles bool // use files regardless of +build lines, file names 无论+build行、文件名如何,都使用文件
Compiler string // compiler to assume when computing target paths 计算目标路径时假定的编译器
BuildTags []string // build constraints to match in +build lines 用于匹配+build行的构建约束
ToolTags []string // toolchain-specific build constraints 特定于工具链的构建约束
ReleaseTags []string // releases the current release is compatible with 当前发布版本兼容的发布版本
InstallSuffix string // suffix to use in the name of the install dir 要在安装目录名称中使用的后缀
}
For more information about the meaning of these fields see the documentation
for the go/build package's Context type.
有关这些字段含义的更多信息,请参阅go/build包的Context类型的文档。
The -json flag causes the package data to be printed in JSON format
instead of using the template format.
-json标志使包数据以JSON格式而不是使用模板格式打印。
The -compiled flag causes list to set CompiledGoFiles to the Go source
files presented to the compiler. Typically this means that it repeats
the files listed in GoFiles and then also adds the Go code generated
by processing CgoFiles and SwigFiles. The Imports list contains the
union of all imports from both GoFiles and CompiledGoFiles.
-compiled标志让list将CompiledGoFiles设置为呈现给编译器的Go源文件。通常这意味着它会重复GoFiles中列出的文件,然后还会添加通过处理CgoFiles和SwigFiles生成的Go代码。Imports列表包含来自GoFiles和CompiledGoFiles的所有导入的并集。
The -deps flag causes list to iterate over not just the named packages
but also all their dependencies. It visits them in a depth-first post-order
traversal, so that a package is listed only after all its dependencies.
Packages not explicitly listed on the command line will have the DepOnly
field set to true.
-deps标志导致list不仅遍历给出名字的包,还遍历它们的所有依赖项。它以深度优先的后序遍历方式访问它们,因此包仅在其所有依赖项之后列出。
将DepOnly字段设置为true的包不会在命令行上明确列出。
The -e flag changes the handling of erroneous packages, those that
cannot be found or are malformed. By default, the list command
prints an error to standard error for each erroneous package and
omits the packages from consideration during the usual printing.
With the -e flag, the list command never prints errors to standard
error and instead processes the erroneous packages with the usual
printing. Erroneous packages will have a non-empty ImportPath and
a non-nil Error field; other information may or may not be missing
(zeroed).
-e标志更改对错误包的处理方式,即那些无法找到或格式错误的包。默认情况下,list命令为每个错误的包打印一个错误到标准误,并在通常的打印过程中忽略这些包。
使用-e标志,list命令永远不会将错误打印到标准误,而是使用通常的打印来处理错误的包。错误的包将有一个非空的ImportPath和一个非nil的错误字段;其他信息可能会或可能不会丢失(归零)。
The -export flag causes list to set the Export field to the name of a
file containing up-to-date export information for the given package.
-export标志使list将Export字段设置为包含给定包的最新导出信息的文件名。
The -find flag causes list to identify the named packages but not
resolve their dependencies: the Imports and Deps lists will be empty.
-find标志让list识别给出名字的包但不解析它们的依赖关系:Imports和Deps的列表将为空。
The -test flag causes list to report not only the named packages
but also their test binaries (for packages with tests), to convey to
source code analysis tools exactly how test binaries are constructed.
The reported import path for a test binary is the import path of
the package followed by a ".test" suffix, as in "math/rand.test".
When building a test, it is sometimes necessary to rebuild certain
dependencies specially for that test (most commonly the tested
package itself). The reported import path of a package recompiled
for a particular test binary is followed by a space and the name of
the test binary in brackets, as in "math/rand [math/rand.test]"
or "regexp [sort.test]". The ForTest field is also set to the name
of the package being tested ("math/rand" or "sort" in the previous
examples).
-test标志让list不仅报告给出名字的包,还报告它们的测试二进制文件(对于带有测试的包),以准确地向源代码分析工具传达测试二进制文件的构建方式。
报告的测试二进制文件的导入路径是包的导入路径,后跟“.test”后缀,例如“math/rand.test”。
在构建测试时,有时需要专门为该测试重建某些依赖项(最常见的是测试包本身)。 为特定测试二进制文件重新编译的包的报告的导入路径后跟一个空格和方括号中的测试二进制文件的名称,例如“math/rand [math/rand.test]”或“regexp [sort.test]”。ForTest字段也设置为正在测试的包的名称(前面示例中的“math/rand”或“sort”)。
The Dir, Target, Shlib, Root, ConflictDir, and Export file paths
are all absolute paths.
Dir、Target、Shlib、Root、ConflictDir、Export的文件路径都是绝对路径。
By default, the lists GoFiles, CgoFiles, and so on hold names of files in Dir
(that is, paths relative to Dir, not absolute paths).
The generated files added when using the -compiled and -test flags
are absolute paths referring to cached copies of generated Go source files.
Although they are Go source files, the paths may not end in ".go".
默认情况下,GoFiles、CgoFiles等列表包含Dir中文件的名称(即相对于Dir的路径,而不是绝对路径)。
使用-compiled和-test标志时添加的生成文件使用绝对路径,指向的是生成的Go源文件的缓存副本。
虽然它们是Go源文件,但路径可能不会以“.go”结尾。
The -m flag causes list to list modules instead of packages.
-m标志让list列出模块而不是包。
When listing modules, the -f flag still specifies a format template
applied to a Go struct, but now a Module struct:
列出模块时,-f标志仍然指定应用于一个Go结构体的格式模板,但现在是一个模块(Module)结构:
type Module struct {
Path string // module path 模块路径
Version string // module version 模块版本号
Versions []string // available module versions (with -versions) 可用的模块版本(使用-versions选项)
Replace *Module // replaced by this module 被这个模块替代
Time *time.Time // time version was created 版本被创建的时刻
Update *Module // available update, if any (with -u) 可用的更新,如果可用的话(使用-u选项)
Main bool // is this the main module? 这是主模块吗?
Indirect bool // is this module only an indirect dependency of main module? 这个模块只是主模块的一个间接依赖项吗?
Dir string // directory holding files for this module, if any 包含这个模块的文件的目录,如果有的话
GoMod string // path to go.mod file used when loading this module, if any 当加载这个模块的时候使用的go.mod文件的路径,如果有的话
GoVersion string // go version used in module 在模块里使用的go版本
Retracted string // retraction information, if any (with -retracted or -u) 撤回相关信息,如果有的话(使用-retracted或-u选项)
Error *ModuleError // error loading module 加载模块错误
}
type ModuleError struct {
Err string // the error itself 错误本身
}
The file GoMod refers to may be outside the module directory if the
module is in the module cache or if the -modfile flag is used.
如果模块在模块缓存中或使用了-modfile标志,则GoMod引用的文件可能位于模块目录之外。
The default output is to print the module path and then
information about the version and replacement if any.
For example, 'go list -m all' might print:
默认打印输出模块路径,然后是版本号和替换(如果有的话)相关的信息。
例如,“go-list-mall”可能会打印:
my/main/module
golang.org/x/text v0.3.0 => /tmp/text
rsc.io/pdf v0.1.1
The Module struct has a String method that formats this
line of output, so that the default format is equivalent
to -f '{{.String}}'.
Module结构体有一个String方法来格式化这行输出,因此默认格式相当于-f '{{.String}}'。
Note that when a module has been replaced, its Replace field
describes the replacement module, and its Dir field is set to
the replacement's source code, if present. (That is, if Replace
is non-nil, then Dir is set to Replace.Dir, with no access to
the replaced source code.)
请注意,当模块被替换时,其Replace字段描述替换模块,并且其Dir字段设置为替换模块的源代码(如果存在)。(也就是说,如果Replace为非nil,则Dir设置为Replace.Dir,无法访问被替换的源代码。)
The -u flag adds information about available upgrades.
When the latest version of a given module is newer than
the current one, list -u sets the Module's Update field
to information about the newer module. list -u will also set
the module's Retracted field if the current version is retracted.
The Module's String method indicates an available upgrade by
formatting the newer version in brackets after the current version.
If a version is retracted, the string "(retracted)" will follow it.
For example, 'go list -m -u all' might print:
-u标志添加有关可用升级的信息。
当给定模块的最新版本比当前版本新时,list -u将模块的Update字段设置为有关较新模块的信息。如果当前版本被撤回,list -u还将设置模块的Retracted字段。
模块的String方法通过在当前版本之后的方括号中格式化新版本来指示可用的升级。
如果一个版本被撤回,字符串“(retracted)”将跟随它。
例如,'go list -m -u all'可能会打印:
my/main/module
golang.org/x/text v0.3.0 [v0.4.0] => /tmp/text
rsc.io/pdf v0.1.1 (retracted) [v0.1.2]
(For tools, 'go list -m -u -json all' may be more convenient to parse.)
(对于工具,“go list -m -u -json all”的输出可能更便于解析。)
The -versions flag causes list to set the Module's Versions field
to a list of all known versions of that module, ordered according
to semantic versioning, earliest to latest. The flag also changes
the default output format to display the module path followed by the
space-separated version list.
-versions标志使list将Module的versions字段设置为该模块的所有已知版号本的列表,这些版本根据语义版本号从最早到最新进行排序。该标志还将更改默认输出格式,以显示模块路径,后跟空格分隔的版本号列表。
The -retracted flag causes list to report information about retracted
module versions. When -retracted is used with -f or -json, the Retracted
field will be set to a string explaining why the version was retracted.
The string is taken from comments on the retract directive in the
module's go.mod file. When -retracted is used with -versions, retracted
versions are listed together with unretracted versions. The -retracted
flag may be used with or without -m.
-retracted标志让list报告有关撤回的模块的版本信息。当-retracted与-f或-json一起使用时,Retracted字段将被设置为一个字符串,解释版本被撤回的原因。
该字符串取自模块的go.mod文件中retract指令的注释。当-retracted与-versions一起使用时,撤回的版本与未撤回的版本一起列出。-retracted标志可以与-m一起使用,也可以不与-m一起使用。
The arguments to list -m are interpreted as a list of modules, not packages.
The main module is the module containing the current directory.
The active modules are the main module and its dependencies.
With no arguments, list -m shows the main module.
With arguments, list -m shows the modules specified by the arguments.
Any of the active modules can be specified by its module path.
The special pattern "all" specifies all the active modules, first the main
module and then dependencies sorted by module path.
A pattern containing "..." specifies the active modules whose
module paths match the pattern.
A query of the form path@version specifies the result of that query,
which is not limited to active modules.
See 'go help modules' for more about module queries.
list -m的参数被解释为模块的列表,而不是包的列表。
主(main)模块是包含当前目录的模块。
活动(active)模块是主模块及其依赖项。
没有给出参数时,list -m显示主模块。
给出参数时,list -m显示参数指定的模块。
任何活动模块都可以通过其模块路径指定。
特殊模式“all”指定所有活动模块,首先是主模块,然后是按模块路径排序的依赖项。
包含“...”的模式指定其模块路径与该模式匹配的活动模块。
path@version形式的查询指定该查询的结果,它不限于活动模块。
有关模块查询的更多信息,请参阅“go help modules”。
The template function "module" takes a single string argument
that must be a module path or query and returns the specified
module as a Module struct. If an error occurs, the result will
be a Module struct with a non-nil Error field.
模板函数“module”接受一个字符串参数,该参数必须是模块路径或查询,并将指定的模块作为Module结构体实例返回。如果发生错误,结果将是一个带有非nil值的Error字段的Module结构体实例。
For more about build flags, see 'go help build'.
有关构建标志的详细信息,请参阅“go help build”。
For more about specifying packages, see 'go help packages'.
有关指定包的详细信息,请参阅“go help packages”。
For more about modules, see https://golang.org/ref/mod.
有关模块的更多信息,请参阅https://golang.org/ref/mod.
go mod模块维护
$ go help mod
Go mod provides access to operations on modules.
Go mod提供对模块操作的访问。
Note that support for modules is built into all the go commands,
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.
请注意,所有go命令都内置了对模块的支持,而不仅仅是“go mod”。例如,日常添加、删除、升级和降级依赖项应该使用“go get”来完成。
有关模块功能的概述,请参阅“go help modules”。
Usage:
用法:
go mod <command> [arguments]
The commands are:
命令有:
download download modules to local cache 下载模块到本地缓存
edit edit go.mod from tools or scripts 从工具或脚本编辑go.mod
graph print module requirement graph 打印输出模块依赖图
init initialize new module in current directory 在当前目录下初始化一个新模块
tidy add missing and remove unused modules 添加缺少的模块并删除未被使用的模块
vendor make vendored copy of dependencies 拷贝依赖项到vendor目录
verify verify dependencies have expected content 验证依赖项是否具有预期的内容
why explain why packages or modules are needed 解释为什么需要这些包或模块
Use "go help mod <command>" for more information about a command.
对于其中某一个命令,使用"go help mod <command>"查看它的详细信息。
对于其中某一个命令,查看go.mod英文文文档或go.mod中文文档查看它的更详细的信息。
go work工作空间维护
$ go help work
Work provides access to operations on workspaces.
work提供对工作空间的操作的访问。
Note that support for workspaces is built into many other commands, not
just 'go work'.
请注意,对工作空间的支持内置于许多其他命令中,而不只是'go work'。
See 'go help modules' for information about Go's module system of which
workspaces are a part.
有关Go的模块系统的信息,请参阅“go help modules”,其中工作空间是其中的一部分。
See https://go.dev/ref/mod#workspaces for an in-depth reference on
workspaces.
有关工作空间的深入参考,请参阅https://go.dev/ref/mod#workspaces。
See https://go.dev/doc/tutorial/workspaces for an introductory
tutorial on workspaces.
有关工作空间的介绍性教程,请参阅https://go.dev/doc/tutorial/workspaces。
A workspace is specified by a go.work file that specifies a set of
module directories with the "use" directive. These modules are used as
root modules by the go command for builds and related operations. A
workspace that does not specify modules to be used cannot be used to do
builds from local modules.
工作空间由go.work文件指定,该文件使用“use”指令指定一组模块目录。这些模块被go命令用作构建和相关操作的根模块。未指定要使用的本地模块的工作空间不能使用本地模块进行构建。
go.work files are line-oriented. Each line holds a single directive,
made up of a keyword followed by arguments. For example:
go.work文件是面向行的。每行包含一个指令,由关键字和参数组成。例如:
go 1.18
use ../foo/bar
use ./baz
replace example.com/foo v1.2.3 => example.com/bar v1.4.5
The leading keyword can be factored out of adjacent lines to create a block,
like in Go imports.
前导关键字可以从相邻行中规约出来以创建一个块,就像在Go import中一样。
use (
../foo/bar
./baz
)
The use directive specifies a module to be included in the workspace's
set of main modules. The argument to the use directive is the directory
containing the module's go.mod file.
use指令指定要包含在工作空间的主模块集合中的模块。use指令的参数是包含该模块的go.mod文件的目录的路径。
The go directive specifies the version of Go the file was written at. It
is possible there may be future changes in the semantics of workspaces
that could be controlled by this version, but for now the version
specified has no effect.
go指令指定编写文件时使用的Go版本。将来此版本控制的工作空间的语义可能发生变化,但目前指定的版本没有影响。
The replace directive has the same syntax as the replace directive in a
go.mod file and takes precedence over replaces in go.mod files. It is
primarily intended to override conflicting replaces in different workspace
modules.
replace指令与go.mod文件中的replace指令具有相同的语法,并且优先于go.mod文件中的替换。它主要用于重写不同工作空间模块中的冲突的替换。
To determine whether the go command is operating in workspace mode, use
the "go env GOWORK" command. This will specify the workspace file being
used.
要确定go命令是否在工作空间模式下运行,请使用“go env GOWORK”命令。这将指定正在使用的工作空间文件。
Usage:
用法:
go work <command> [arguments]
The commands are:
命令有:
edit edit go.work from tools or scripts 从工具或脚本编辑go.work
init initialize workspace file 初始化工作空间文件
sync sync workspace build list to modules 同步工作空间构建列表到模块
use add modules to workspace file 添加模块到工作空间文件
Use "go help work <command>" for more information about a command.
使用“go help work <command>”获取有关命令的更多信息。