原文鏈接 https://golang.org/cmd/go/
由于內(nèi)容過長蚤告,分兩次發(fā)布
Go 是一個用來管理Go源碼的工具座掘。
用法:
go <command> [arguments]
其中command包含以下
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 download and install packages and dependencies
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
使用 "go help <command>"查看命令的更多使用幫助。
更多幫助主題:
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
packages package lists and patterns
testflag testing flags
testfunc testing functions
使用"go help <topic>"查看相關(guān)主題的更多幫助信息途事。
Start a bug report
用法
go bug
Bug打開默認(rèn)瀏覽器啟動新的錯誤報告玄柠,該報告包含有用的系統(tǒng)信息。(譯注:不知有何用)
Compile packages and dependencies
用法:
go build [-o output] [-i] [build flags] [package]
Build 會編譯以導(dǎo)入路徑命名的包及其依賴項咱圆,但是不會安裝結(jié)果笛辟。
如果構(gòu)建的參數(shù)是.go文件列表,則build會將它們視為指定單個包的源文件列表序苏。
編譯單個主程序main包時手幢,build生成的可執(zhí)行文件將以第一個源文件命名(“go build ed.go rx.go”生成"ed"或"ed.exe")或以源代碼目錄命名 ("go build unix/sam"生成"sam" or "sam.exe")其中".exe"后綴是window可執(zhí)行文件后綴。
當(dāng)編譯多個包或單個非main包時忱详,build編譯包但丟棄生成的對象围来,僅用于檢查是否可以構(gòu)建包。
當(dāng)編譯包時,構(gòu)建會忽略以"_test.go"結(jié)尾的文件监透。
-o 標(biāo)記僅在編譯單個包時生效桶错,強制build將生成的可執(zhí)行文件或?qū)ο髮懭胍浴皁utput”命名的文件,而不是上面兩端描述的默認(rèn)行為胀蛮。
-i 標(biāo)記安裝作為膜表依賴性的包院刁。
build flags的可選內(nèi)容與 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 the number of CPUs available.
-race
enable data race detection.
Supported only on linux/amd64, freebsd/amd64, darwin/amd64 and windows/amd64.
-msan
enable interoperation with memory sanitizer.
Supported only on linux/amd64, linux/arm64
and only with Clang/LLVM as the host C compiler.
-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.
-buildmode mode
build mode to use. See 'go help buildmode' for more.
-compiler name
name of compiler to use, as in runtime.Compiler (gccgo or gc).
-gccgoflags '[pattern=]arg list'
arguments to pass on each gccgo compiler/linker invocation.
-gcflags '[pattern=]arg list'
arguments to pass on each go tool compile invocation.
-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
flag. Using a -buildmode option that requires non-default compile flags
has a similar effect.
-ldflags '[pattern=]arg list'
arguments to pass on each go tool link invocation.
-linkshared
link against shared libraries previously created with
-buildmode=shared.
-mod mode
module download mode to use: readonly or vendor.
See 'go help modules' for more.
-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.
-tags 'tag list'
a space-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.
-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>'.
-asmflags,-gccgoflags粪狼,-gcflags和-ldflags標(biāo)志接受以空格分隔的參數(shù)列表退腥,以在構(gòu)建期間傳遞給基礎(chǔ)工具。要在列表中的元素中嵌入空格再榄,請使用單引號或雙引號將其括起來狡刘。參數(shù)列表可以在包模式和等號之后,這限制了該參數(shù)列表只構(gòu)建匹配該模式的包(有關(guān)包模式的描述困鸥,請參閱'go help packages')嗅蔬。如果沒有模式,參數(shù)列表僅適用于命令行上指定的包疾就±绞酰可以用不同的模式重復(fù)標(biāo)志,以便為不同的包組指定不同的參數(shù)虐译。如果包與多個標(biāo)志中給出的模式匹配瘪板,則命令行上將以命令行最后一個匹配的標(biāo)記為準(zhǔn)。例如漆诽,'go build -gcflags=-S fmt' 只輸出fmt包的拆解侮攀,'go build -gcflags=all=-S fmt'輸出fmt的拆解,以及它的依賴厢拭。
有關(guān)指定包的更多信息兰英,請參閱“go help packages”。有關(guān)安裝包和二進制文件的更多信息供鸠,請運行'go help gopath'畦贸。有關(guān)在Go和C / C ++之間調(diào)用的更多信息,請運行'go help c'楞捂。
注意:Build符合某些約定薄坏,例如'go help gopath'所描述的約定。但是寨闹,并非所有項目都遵循這些慣例胶坠。具有自己的約定或使用單獨的軟件構(gòu)建系統(tǒng)的安裝可以選擇使用較低級別的調(diào)用,例如“go tool compile”和“go tool link”繁堡,以避免構(gòu)建工具的一些開銷和設(shè)計決策沈善。另見:go install乡数,go get,go clean闻牡。
Remove object files and cached files(移除對象和緩存文件)
使用
go clean [clean flags] [build flags] [packages]
Clean命令從包源碼路徑刪除對象文件净赴。go命令在臨時構(gòu)建許多臨時對象,所以go clean主要關(guān)注其它工具或手動調(diào)用go build遺留的對象文件罩润。
具體來說玖翅,clean從與導(dǎo)入路徑對應(yīng)的每個源文件刪除以下文件。
_obj/ old object directory, left from Makefiles
_test/ old test directory, left from Makefiles
_testmain.go old gotest file, left from Makefiles
test.out old test log, left from Makefiles
build.out old test log, left from Makefiles
*.[568ao] object files, left from Makefiles
DIR(.exe) from go build
DIR.test(.exe) from go test -c
MAINFILE(.exe) from go build MAINFILE.go
*.so from SWIG
列表中的DIR表示目錄中最終路徑的元素哨啃,MAINFILE 是構(gòu)建包是未包含的目錄中g(shù)o源文件的基本名稱烧栋。
-i 標(biāo)記引導(dǎo)clean移除相應(yīng)的已安裝文檔或二進制文件(go install創(chuàng)建的)
-n 標(biāo)記引導(dǎo)clean輸出將要執(zhí)行的移除命令写妥,但是不執(zhí)行他們拳球。
-r 標(biāo)記引導(dǎo)clean遞歸的應(yīng)用在導(dǎo)入路徑所在包的所有依賴上。
-x 標(biāo)記引導(dǎo)clean輸出執(zhí)行的移除命令
-cache 標(biāo)記引導(dǎo)clean移除go build的所有緩存
-testcache 標(biāo)記引導(dǎo)clean讓go構(gòu)建緩存的測試結(jié)果失效
-modcache 標(biāo)記引導(dǎo)clean移除所有下載模塊的緩存珍特,包括不同版本依賴的解壓源代碼
有關(guān)構(gòu)建標(biāo)志的更多信息祝峻,請參閱“go help build”。
有關(guān)指定包的更多信息扎筒,請參閱“go help packages”莱找。
Show documentation for package or symbol
用法:
go doc [-u] [-c] [package|[package.]symbol[.methodOrField]]
【省略...】
Print Go environment information
用法:
go env [-json] [var ..]
Env輸出Go環(huán)境信息。
默認(rèn)情況下env以shell腳本格式輸出信息(window是批處理文件batch file)嗜桌。如果給出一個或多個的變量名作為參數(shù)奥溺,則env為每個變量名輸出一行值信息。
-json 標(biāo)記以json格式輸出
有關(guān)環(huán)境變量的更多信息骨宠,請參閱“go help environment”浮定。
Update packages to use new APIs
用法:
go fix [packages]
Fix在導(dǎo)入路徑命名的包上運行Go fix命令。
有關(guān)修復(fù)的更多信息层亿,請參閱“go doc cmd / fix”桦卒。
有關(guān)指定包的更多信息,請參閱“go help packages”匿又。
要使用特定選項運行修復(fù)方灾,請運行“go tool fix”。另見:go fmt碌更,go vet裕偿。
Gofmt (reformat) package sources(源碼格式化)
用法:
go fmt [-n] [-x] [packages]
fmt 在以導(dǎo)入路徑命名的包上 運行命令"gofmt -l -w"。它輸出那些被修改的文件名痛单。
有關(guān)gofmt的更多信息嘿棘,請參閱“go doc cmd / gofmt”。
有關(guān)指定包的更多信息桦他,請參閱“go help packages”蔫巩。
-n標(biāo)志打印將要執(zhí)行的命令谆棱。-x標(biāo)志在執(zhí)行時打印命令。
要使用特定選項運行g(shù)ofmt圆仔,請運行g(shù)ofmt本身垃瞧。
另見:go fix,go vet坪郭。
Generate Go files by processing source
用法:
go generate [-run regexp] [-n] [-v] [-x] [build flags] [file.go... | packages]
generate 運行現(xiàn)有文件中的注釋描述个从。那些注釋可以執(zhí)行任何程序,不過一般意圖是創(chuàng)建或更新go源碼文件歪沃。
go generate 不會被go build, go get, go test等命令觸發(fā)嗦锐,它需要明確的指定運行。
go generate 掃描文件中的指令沪曙,指令格式如下:
//go:generate command argument...
(注:沒有前置空格奕污,"http://go"中也沒有空格)generate執(zhí)行指令的地方,對應(yīng)一個可本地執(zhí)行的可執(zhí)行文件液走。它必須位于shell路徑(gofmt)碳默,完整的路徑或是命令別名,如下所述:
為了向人類與工具傳達代碼是生成的缘眶,生成的源應(yīng)該具有與以下正則表達式匹配的行(在Go語法中):
// Code generated .* DO NOT EDIT.$
該行可能出現(xiàn)在文件中任意地方嘱根,但通常位于開頭附近,因此很容易找到巷懈。
請注意该抒,go generate不會解析文件,因此看起來像注釋或多行字符串中的指令的行將被視為指令顶燕。
該指令的參數(shù)是空格分隔的標(biāo)記或雙引號字符串凑保,它們在運行時作為單獨的參數(shù)傳遞給生成器。
帶引號的字符串使用Go語法并在執(zhí)行前進行評估; 帶引號的字符串作為生成器的單個參數(shù)出現(xiàn)割岛。
Go運行生成器時生成幾個變量:
$GOARCH
The execution architecture (arm, amd64, etc.)
$GOOS
The execution operating system (linux, windows, etc.)
$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.
除了變量替換與字符串評估愉适,在命令行不執(zhí)行類似“globbing”的特殊處理。
作為運行命令之前的最后一步癣漆,任何具有字母數(shù)字名稱的環(huán)境變量(例如 HOME)的調(diào)用都將在整個命令行中進行擴展维咸。變量擴展的語法是所有操作系統(tǒng)上的
NAME將擴展為空字符串婚肆。
指令格式如下
//go:generate -command xxx args...
僅在此源文件剩余的部分租副,指定字符串xxx表示有args標(biāo)識的命令。這可以用于創(chuàng)建別名或者處理多字生成器较性。例如:
//go:generate -command foo go tool foo
指定命令“foo”表示生成器“go tool foo”
Generate 按照命令行給定的順序處理包用僧,每次執(zhí)行一個结胀。如果命令行列出.go文件,他們被視為同一個包责循。在包內(nèi)糟港,generate以文件名順序處理源文件,每次執(zhí)行一個院仿。在文件中秸抚,generate 按照文件中出現(xiàn)的順序執(zhí)行生成器,每次一個歹垫。
如果任何生成器返回錯誤退出狀態(tài)剥汤,“go generate”將跳過該包的所有后續(xù)處理。
生成器在包的源目錄中運行排惨。
Go generate接受一個特定的標(biāo)志:
-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.
它還接受標(biāo)準(zhǔn)構(gòu)建標(biāo)志吭敢,包括-v,-n和-x若贮。-v標(biāo)志在處理包時打印包和文件的名稱省有。-n標(biāo)志打印將要執(zhí)行的命令痒留。-x標(biāo)志在執(zhí)行時打印命令谴麦。
有關(guān)構(gòu)建標(biāo)志的更多信息,請參閱“go help build”伸头。
有關(guān)指定包的更多信息匾效,請參閱“go help packages”。
Download and install packages and dependencies(下載安裝包依賴)
用法:
go get [-d] [-f] [-t] [-u] [-v] [-fix] [-insecure] [build flags] [packages]
get下載導(dǎo)入路徑指定的包及其依賴項恤磷。然后像“goinstall”那樣安裝下載的包面哼。
-d 標(biāo)記指示在下載軟件包后停止; 也就是說,它指示不安裝軟件包扫步。
-f 標(biāo)記僅在設(shè)置-u時有效魔策,強制get -u不驗證每個包是否已從其導(dǎo)入路徑隱含的源代碼倉儲中檢出。如果源是original的本地分支河胎,這可能很有用闯袒。
-fix 標(biāo)記指示get在解析依賴項或構(gòu)建代碼之前在下載的包上運行修復(fù)工具。
-insecure 標(biāo)志允許從存儲庫中提取并使用不安全的方案(如HTTP)解析自定義域游岳。謹(jǐn)慎使用政敢。
-t 標(biāo)記指示get還下載構(gòu)建指定包的測試所需的包。
-u 標(biāo)記指示get使用網(wǎng)絡(luò)更新命名包及其依賴項胚迫。默認(rèn)情況下喷户,get使用網(wǎng)絡(luò)檢出缺少的包,但不使用它來查找現(xiàn)有包的更新访锻。
-v 標(biāo)記啟用詳細進度和調(diào)試輸出褪尝。
Get還接受構(gòu)建標(biāo)志來控制安裝闹获。請參閱'go help build'掰曾。
簽出新包時雹顺,get創(chuàng)建目標(biāo)目錄GOPATH / src / <import-path>。如果GOPATH包含多個條目饭冬,則get使用第一個條目灾馒。有關(guān)詳細信息茎用,請參閱:'go help gopath'。
簽出或更新包時睬罗,查找與本地安裝的Go版本匹配的分支或標(biāo)記轨功。最重要的規(guī)則是,如果本地安裝運行版本為“go1”容达,則搜索名為“go1”的分支或標(biāo)記古涧。如果不存在此類版本,則會檢索包的默認(rèn)分支花盐。
當(dāng)獲取簽出或更新Git存儲庫時羡滑,它還會更新存儲庫引用的任何git子模塊。
永遠不會檢出或更新存儲在供應(yīng)商目錄中的代碼算芯。
有關(guān)指定包的更多信息柒昏,請參閱“go help packages”。
有關(guān)“go get”如何找到要下載的源代碼的更多信息熙揍,請參閱“go help importpath”职祷。
本文描述了使用GOPATH管理源代碼和依賴項時get的行為。如果go命令在模塊感知模式下運行届囚,則get的標(biāo)志和效果的細節(jié)會發(fā)生變化有梆,就像'go help get'一樣。請參閱“go help modules”和“go help module-get”意系。另見:go build泥耀,go install,go clean蛔添。
Compile and install packages and dependencies(編譯安裝包及其依賴)
用法:
go install [-i] [build flags] [packages]
install 編譯安裝導(dǎo)入路徑命名的包痰催。
-i 標(biāo)記安裝命名包的依賴項。
有關(guān)構(gòu)建標(biāo)志的更多信息作郭,請參閱“go help build”陨囊。
有關(guān)指定包的更多信息,請參閱“go help packages”夹攒。
另見:go build蜘醋,go get,go clean咏尝。
List packages or modules
用法:
go list [-f format] [-json] [-m] [list flags] [build flags] [packages]
list命令列出命名的包压语,每個包一行啸罢,最常使用的標(biāo)記是 -f 和 -json,這兩個標(biāo)記用來控制輸出的包的格式胎食。其他list的標(biāo)記扰才,控制更具體的細節(jié)。
默認(rèn)輸出顯示包的導(dǎo)入路徑:
bytes
encoding/json
github.com/gorilla/mux
golang.org/x/net/html
-f 標(biāo)記使用包模板語法指定列表的格式厕怜,默認(rèn)輸出格式相當(dāng)于 -f'{.ImportPath}'衩匣。模板結(jié)構(gòu)體如下:
// A Package describes a single package found in a directory.
type Package struct {
PackagePublic // visible in 'go list'
Internal PackageInternal // for use inside go command only
}
type PackagePublic struct {
// Note: These fields are part of the go command's public API.
// See list.go. It is okay to add fields, but not to change or
// remove existing ones. Keep in sync with list.go
Dir string `json:",omitempty"` // directory containing package sources
ImportPath string `json:",omitempty"` // import path of package in dir
ImportComment string `json:",omitempty"` // path in import comment on package statement
Name string `json:",omitempty"` // package name
Doc string `json:",omitempty"` // package documentation string
Target string `json:",omitempty"` // installed target for this package (may be executable)
Shlib string `json:",omitempty"` // the shared library that contains this package (only set when -linkshared)
Root string `json:",omitempty"` // Go root or Go path dir containing this package
ConflictDir string `json:",omitempty"` // Dir is hidden by this other directory
ForTest string `json:",omitempty"` // package is only for use in named test
Export string `json:",omitempty"` // file containing export data (set by go list -export)
Module *modinfo.ModulePublic `json:",omitempty"` // info about package's module, if any
Match []string `json:",omitempty"` // command-line patterns matching this package
Goroot bool `json:",omitempty"` // is this package found in the Go root?
Standard bool `json:",omitempty"` // is this package part of the standard Go library?
DepOnly bool `json:",omitempty"` // package is only as a dependency, not explicitly listed
BinaryOnly bool `json:",omitempty"` // package cannot be recompiled
Incomplete bool `json:",omitempty"` // was there an error loading this package or dependencies?
// Stale and StaleReason remain here *only* for the list command.
// They are only initialized in preparation for list execution.
// The regular build determines staleness on the fly during action execution.
Stale bool `json:",omitempty"` // would 'go install' do anything for this package?
StaleReason string `json:",omitempty"` // why is Stale true?
// Source files
// If you add to this list you MUST add to p.AllFiles (below) too.
// Otherwise file name security lists will not apply to any new additions.
GoFiles []string `json:",omitempty"` // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles)
CgoFiles []string `json:",omitempty"` // .go source files that import "C"
CompiledGoFiles []string `json:",omitempty"` // .go output from running cgo on CgoFiles
IgnoredGoFiles []string `json:",omitempty"` // .go source files ignored due to build constraints
CFiles []string `json:",omitempty"` // .c source files
CXXFiles []string `json:",omitempty"` // .cc, .cpp and .cxx source files
MFiles []string `json:",omitempty"` // .m source files
HFiles []string `json:",omitempty"` // .h, .hh, .hpp and .hxx source files
FFiles []string `json:",omitempty"` // .f, .F, .for and .f90 Fortran source files
SFiles []string `json:",omitempty"` // .s source files
SwigFiles []string `json:",omitempty"` // .swig files
SwigCXXFiles []string `json:",omitempty"` // .swigcxx files
SysoFiles []string `json:",omitempty"` // .syso system object files added to package
// Cgo directives
CgoCFLAGS []string `json:",omitempty"` // cgo: flags for C compiler
CgoCPPFLAGS []string `json:",omitempty"` // cgo: flags for C preprocessor
CgoCXXFLAGS []string `json:",omitempty"` // cgo: flags for C++ compiler
CgoFFLAGS []string `json:",omitempty"` // cgo: flags for Fortran compiler
CgoLDFLAGS []string `json:",omitempty"` // cgo: flags for linker
CgoPkgConfig []string `json:",omitempty"` // cgo: pkg-config names
// Dependency information
Imports []string `json:",omitempty"` // import paths used by this package
ImportMap map[string]string `json:",omitempty"` // map from source import to ImportPath (identity entries omitted)
Deps []string `json:",omitempty"` // all (recursively) imported dependencies
// Error information
// Incomplete is above, packed into the other bools
Error *PackageError `json:",omitempty"` // error loading this package (not dependencies)
DepsErrors []*PackageError `json:",omitempty"` // errors loading dependencies
// Test information
// If you add to this list you MUST add to p.AllFiles (below) too.
// Otherwise file name security lists will not apply to any new additions.
TestGoFiles []string `json:",omitempty"` // _test.go files in package
TestImports []string `json:",omitempty"` // imports from TestGoFiles
XTestGoFiles []string `json:",omitempty"` // _test.go files outside package
XTestImports []string `json:",omitempty"` // imports from XTestGoFiles
}
存在 vendor 目錄下的包得到的 ImportPath 包含 vendor 路徑(例如: "d/vendor/p" 而不僅僅是“p”),所以ImportPath 可以唯一標(biāo)記一個給定包的副本。Imports, Deps, TestImports, 與 XTestImports 列表也包含了擴展導(dǎo)入路徑粥航。查閱 golang.org/s/go15vendor 了解更多細節(jié)琅捏。
錯誤信息結(jié)構(gòu)如下:
// A PackageError describes an error loading information about a package.
type PackageError struct {
ImportStack []string // shortest path from package named on command line to this one
Pos string // position of error
Err string // the error itself
IsImportCycle bool `json:"-"` // the error is an import cycle
Hard bool `json:"-"` // whether the error is soft or hard; soft errors are ignored in some places
}
模塊信息是一個 Module 結(jié)構(gòu)體,如下文 list -m
模板方法“join”調(diào)用 strings.Join
模板方法 “context”返回構(gòu)建上下文递雀,定義如下:
type Context struct {
GOARCH string // target architecture
GOOS string // target operating system
GOROOT string // Go root
GOPATH string // Go path
CgoEnabled bool // whether cgo can be used
UseAllFiles bool // use files regardless of +build lines, file names
Compiler string // compiler to assume when computing target paths
BuildTags []string // build constraints to match in +build lines
ReleaseTags []string // releases the current release is compatible with
InstallSuffix string // suffix to use in the name of the install dir
}
更多關(guān)于 Context 結(jié)構(gòu)體字段的信息柄延,查看go/build 包的 Context 類型文檔。
-json 標(biāo)記指示包數(shù)據(jù)使用 JSON格式輸出缀程,而不是模板類型搜吧。
-compiled 標(biāo)記指示list 將 CompiledGoFiles 設(shè)置為呈現(xiàn)給編譯器的Go源文件。通常這意味著它會重復(fù)GoFiles中列出的文件杨凑,然后還會添加通過CgoFiles 和 SwigFiles 生成的Go代碼滤奈。Import列表包含來自GoFiles與CompiledGoFiles的所有導(dǎo)入合集。
-deps 標(biāo)記指示list不僅是迭代命名的包蠢甲,還迭代所有它們的依賴僵刮。list以深度優(yōu)先順序遍歷它們,所以包會在其所有依賴之后列出鹦牛。未在命令行明確列出的包將DepOnly字段設(shè)置為true
-e 標(biāo)記改變錯誤包的處理方式,錯誤包指那些找不到或錯誤的包勇吊。默認(rèn)情況下曼追,list命令為每個錯誤的包打印一個標(biāo)準(zhǔn)錯誤,并通常在打印過程中忽略它們汉规。使用-e標(biāo)記礼殊,list命令將不會打印錯誤信息,而是像正常情況打印那些錯誤的包针史。錯誤的包郵非空的ImportPath和 non-nil的 Error 字段晶伦;其它信息可能會是丟失(零值)
-export 標(biāo)記使list 將 Export 字段設(shè)置為包含給定包的最新到處信息的文件的名稱。
-find 標(biāo)記指示list標(biāo)識命名的包啄枕,但是不解析他們的依賴關(guān)系婚陪,Imports 與 Deps列表將為空。
-test 標(biāo)記指示list不止報告命名包而且報告測試二進制文件(對于帶有測試的包)频祝,以準(zhǔn)確地向源代碼分析工具傳達測試二進制文件的構(gòu)造方式泌参。報告的二進制測試文件導(dǎo)入路徑是包的導(dǎo)入路徑后面加“.test”后綴脆淹,如“math/rand.test”。在構(gòu)建測試時沽一,有事需要專門為測試重建某些依賴項(最常見的是測試包本身)盖溺。報告的針對特定的測試二進制文件重新編譯的包的導(dǎo)入路徑后跟一個空格和中括號內(nèi)的二進制文件名稱,如“math/rand [math/rand.test]”或者"regexp [sort.test]"铣缠。ForTest 字段也設(shè)置正在測試的包的名稱(“math/rand”或 "sort")
Dir, Target, Shlib, Root, ConflictDir, and Export 字段中包含的路徑都是絕對路徑.
默認(rèn)情況下烘嘱,GoFiles,CgoFiles等列表保持Dir中的文件名(即相對于Dir的路徑蝗蛙,而不是絕對路徑)拙友。使用-compiled和-test 標(biāo)記時添加的生成文件是引用生成的Go源文件的副本的絕對路徑。雖然它們是Go源文件歼郭,但路徑可能不是以“.go”結(jié)尾遗契。
-m 標(biāo)記指示list列出模塊而不是包
當(dāng)-m指定列出模塊時,使用 -f 仍然可以指定基于特定Go結(jié)構(gòu)體的模板病曾,不過這次是Module 結(jié)構(gòu)體
type Module struct {
Path string // module path
Version string // module version
Versions []string // available module versions (with -versions)
Replace *Module // replaced by this module
Time *time.Time // time version was created
Update *Module // available update, if any (with -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 for this module, if any
Error *ModuleError // error loading module
}
type ModuleError struct {
Err string // the error itself
}
默認(rèn)輸出是打印模塊路徑牍蜂,然后打印有關(guān)版本和替換信息(如果有)。例如“go list -m all”可能會打犹┩俊:
my/main/module
golang.org/x/text v0.3.0 [v0.4.0] => /tmp/text
rsc.io/pdf v0.1.1 [v0.1.2]
Module 結(jié)構(gòu)體有一個String方法鲫竞,用于格式化這一行輸出,所以默認(rèn)格式等同于 -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.)
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. The Module's String method indicates an available upgrade by formatting the newer version in brackets after the current version. For example, 'go list -m -u all' might print:my/main/module
golang.org/x/text v0.3.0 [v0.4.0] => /tmp/text
rsc.io/pdf v0.1.1 [v0.1.2]
(For tools, 'go list -m -u -json all' may be more convenient to parse.)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.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.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.For more about build flags, see 'go help build'.For more about specifying packages, see 'go help packages'.For more about modules, see 'go help modules'.
Module maintenance(模塊維護)
Go mod 提供對模塊的操作
注意逼蒙,所有的go命令都內(nèi)置了對模塊的支持从绘,而不僅僅是“go mod”。例如日常應(yīng)該使用“go get”對依賴項做添加是牢,移除僵井,升級和降級操作。有關(guān)模塊功能的概述驳棱,請參閱“go help modules”批什。
用法:
go mod <command> [arguments]
command 是:
download download modules to local cache
edit edit go.mod from tools or scripts
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
verify verify dependencies have expected content
why explain why packages or modules are needed
有關(guān)命令的更多信息,請使用“go help mod <command>”社搅。
Download modules to local cache(下載模塊到本地緩存)
用法:
go mod download [-json] [modules]
download命令下載命名模塊驻债,模塊是那些可以被模塊模式選擇的主模塊依賴 或者 路徑格式為@version的模塊查詢。如果沒有參數(shù)形葬,download 應(yīng)用于所有依賴項的主模塊
在進行普通操作是go命令將自動下載模塊合呐。“go mod download”一般主要用于對本地緩存的預(yù)填充或者驗證go模塊代理笙以。
默認(rèn)情況下淌实,如果download出現(xiàn)錯誤,則打印標(biāo)準(zhǔn)錯誤信息,沒有則為空翩伪。 -json 標(biāo)記指示download輸出一系列json對象作為標(biāo)準(zhǔn)輸出微猖,用于描述下載模塊(或者失敗) 對應(yīng)此Go結(jié)構(gòu)體
type Module struct {
Path string // module path
Version string // module version
Error string // error loading module
Info string // absolute path to cached .info file
GoMod string // absolute path to cached .mod file
Zip string // absolute path to cached .zip file
Dir string // absolute path to cached source root directory
Sum string // checksum for path, version (as in go.sum)
GoModSum string // checksum for go.mod (as in go.sum)
}
有關(guān)模塊查詢的更多信息缘屹,請參閱“go help modules”凛剥。
Edit go.mod from tools or scripts(使用工具或腳本編輯go.mod)
用法:
go mod edit [editing flags][go.mod]
edit提供了一個命令行界面用于編輯go.mod,主要用于工具或腳本轻姿。他只讀go.mod犁珠;并沒有查找關(guān)于模塊的信息。默認(rèn)情況下edit命令編輯主模塊的go.mod文件互亮,但是可以指定不同的編輯來指定編輯不同文件犁享。
editing flags 參數(shù)指出一系列的編輯操作。
-fmt 編輯重新格式化go.mod而不進行任何更改豹休。這樣的重新格式化在其他修改到go.mod操作的時候也會發(fā)生炊昆。在沒有指定其他標(biāo)記的情況下才需要指定-fmt標(biāo)記。
-module 標(biāo)記改變模塊路徑(go.mod文件中 module 那行)
-require=path@version 與 -droprequire=path 標(biāo)記分別添加威根,移除給定模塊路徑版本的的需求模塊凤巨。注意 -require將覆蓋任何存在的需求模塊的路徑。這兩個標(biāo)記主要用于了解模塊視圖的工具洛搀。用戶更偏好使用"go get path@version" or "go get path@none"這樣可以根據(jù)需要調(diào)整go.mod模塊敢茁,以滿足其他模塊的導(dǎo)入需求。
-require, -droprequire, -exclude, -dropexclude, -replace, and -dropreplace 這些編輯標(biāo)簽可以重復(fù)留美,將按照給出的順序依次做出修改彰檬。
-go=version 設(shè)置期望使用的Go語言版本。
-print 標(biāo)記以text格式輸出最終的go.mod谎砾,而不是將其寫回go.mod
-json 標(biāo)記以JSON格式輸出最終的go.mod文件逢倍,而不是將其寫回go.mod,JSON輸出格式對應(yīng)以下Go類型:
type Module struct {
Path string
Version string
}
type GoMod struct {
Module Module
Go string
Require []Require
Exclude []Module
Replace []Replace
}
type Require struct {
Path string
Version string
Indirect bool
}
type Replace struct {
Old Module
New Module
}
注意:這僅僅描述了go.mod文件本身棺榔,不包括間接參考的模塊瓶堕。對于構(gòu)建可用的完整模塊集,請使用'go list -m -json all'症歇。
例如,工具可以通過解析'go mod edit -json'的輸出來獲取go.mod作為數(shù)據(jù)結(jié)構(gòu)谭梗,然后可以通過使用-require忘晤,-exclude等調(diào)用'go mod edit'來進行更改。
Print module requirement graph(輸出模塊需求視圖)
用法:
go mod graph
graph 輸出模塊需求圖表的文本格式(包括替換內(nèi)容)激捏。每行輸出有由空格隔開的兩部分組成设塔,模塊與其對應(yīng)的需求。每個模塊被格式 path@version 唯一識別远舅,除了主模塊闰蛔,沒有 @version 的版本后綴痕钢。
Initialize new module in current directory(在當(dāng)前路徑初始化模塊)
用法:
go mod init [module]
init 初始化并在當(dāng)前路徑寫入一個新的go.mod文件,實際上是在當(dāng)前路徑創(chuàng)建一個新模塊根(module root)序六。原目錄必須不存在go.mod文件任连。
如果可能的話,init將會根據(jù)import comments 或者 版本控制系統(tǒng)的配置文件推算出模塊路徑例诀。如果要重寫路徑随抠,指定模塊路徑參數(shù)(module)
Add missing and remove unused modules(添加缺失模塊,移除未使用模塊)
用法:
go mod tidy [-v]
tidy 確保 go.mod文件與模塊中源代碼匹配繁涂。tidy添加當(dāng)前模塊包編譯所需的缺失的模塊拱她,移除其它無關(guān)的模塊。該命令同樣添加缺失的實體到go.sum文件扔罪,并從中移除不需要的秉沼。
-v 標(biāo)記指示tidy輸出模塊移除信息以及標(biāo)準(zhǔn)錯誤輸出。
Make vendored copy of dependencies(創(chuàng)建依賴的 vendor復(fù)制)
用法:
go mod vendor [-v]
vendor重置主模塊的vendor目錄矿酵,使其包含所有編譯需要的包唬复,且測試所有主模塊的包(??)。它不包括vendor包的測試代碼
-v 標(biāo)記指示vendor 以標(biāo)準(zhǔn)錯誤 輸出模塊名坏瘩,包名
Verify dependencies have expected content(驗證依賴)
用法:
go mod verify
verify 檢查當(dāng)前模塊的依賴(存儲在本地下載緩存中)自下載后有沒有被改變盅抚。如果所有模塊都沒有被修改,verify 輸出“all modules verified”倔矾,否則將報告哪些模塊被改變妄均,然后導(dǎo)致 go mod 命令以非零狀態(tài)退出。
Explain why packages or modules are needed(解釋為何需要這些包與模塊)
使用:
go mod why [-m] [-vendor] packages...
Why shows a shortest path in the import graph from the main module to each of the listed packages. If the -m flag is given, why treats the arguments as a list of modules and finds a path to any package in each of the modules.By default, why queries the graph of packages matched by "go list all", which includes tests for reachable packages. The -vendor flag causes why to exclude tests of dependencies.The output is a sequence of stanzas, one for each package or module name on the command line, separated by blank lines. Each stanza begins with a comment line "# package" or "# module" giving the target package or module. Subsequent lines give a path through the import graph, one package per line. If the package or module is not referenced from the main module, the stanza will display a single parenthesized note indicating that fact.For example:$ go mod why golang.org/x/text/language golang.org/x/text/encoding
# golang.org/x/text/language
rsc.io/quote
rsc.io/sampler
golang.org/x/text/language
# golang.org/x/text/encoding
(main module does not need package golang.org/x/text/encoding)
$
Compile and run Go program(編譯運行Go程序)
用法:
go run [build flags] [-exec xprog] package [arguments...]
run 編譯運行命名的主Go包哪自。通常 package 參數(shù)被指定為一系列.go源文件丰包,不過它也可能是一個導(dǎo)入路徑,文件系統(tǒng)路徑壤巷,或者模式匹配到一個已知的包邑彪,例如"go run ." 或 "go run my/cmd"
默認(rèn)情況下,go run 命令直接運行編譯的二進制文件:"a.out arguments..."胧华。如果指定 -exec 標(biāo)記寄症,那么“go run”通過xprog調(diào)用二進制文件
'xprog a.out arguments...'
若為指定 -exec 標(biāo)記,GOOS 或 GOARCH 與默認(rèn)系統(tǒng)不一樣矩动,且當(dāng)前搜索路徑可用找到 go_GOARTH_exec 的文件有巧,那么“go run”將通過該程序調(diào)用二進制文件,例如“go_nacl_386_exec a.out arguments...”這允許通過模擬器或其他方式執(zhí)行交叉編譯悲没。
run的退出狀態(tài)碼與 編譯二進制文件的退出狀態(tài)碼不一樣
Test packages(測試包)
用法:
go test [build/test flags] [packages] [build/test flags & test binary flags]
"go test"對導(dǎo)入路徑下的包自動化測試篮迎。它格式化輸出測試結(jié)果的摘要:
ok archive/tar 0.011s
FAIL archive/zip 0.022s
ok compress/gzip 0.033s
...
每個測試失敗的包會有詳細輸出。
“go test”重新編譯包內(nèi)每個匹配“_test.go”的文件。那些額外的文件可以包含函數(shù)測試甜橱,基準(zhǔn)測試逊笆,樣例測試(function,benchmark,example)。參閱“go help testfunc”了解更多岂傲。每個列出的包都會單獨的執(zhí)行測試二進制文件难裆。以" _ " (包括_test.go)或"."開頭的文件將被忽略。
那些以 " _test " 聲明的包將被單獨的編譯譬胎,然后連接并與主測試文件一起運行差牛。
go tool 將忽略名為 testdata的目錄,使其可以用于放置測試需要的輔助數(shù)據(jù)
作為編譯測試文件的一部分堰乔,go test 對包運行 vet偏化,vet檢查測試源文件中的重大問題。如果 go vet 找到任何問題镐侯,那么 go test將報告問題且終止執(zhí)行侦讨。Only a high-confidence subset of the default go vet checks are used. That subset is: 'atomic', 'bool', 'buildtags', 'nilfunc', and 'printf'. You can see the documentation for these and other vet tests via "go doc cmd/vet". To disable the running of go vet, use the -vet=off flag.
所有的測試輸出與匯總都打印到go命令的標(biāo)準(zhǔn)輸出,即使測試打印他們自己的標(biāo)準(zhǔn)錯誤(go命令標(biāo)準(zhǔn)錯誤保留用于構(gòu)建測試時打印錯誤苟翻。)
go test 使用以下兩種方式運行測試:
第一種稱為本地目錄模式韵卤,在 go test 調(diào)用沒有指定包參數(shù)的時候發(fā)生(例如:"go test" "go test -v")。在這個模式下崇猫,go test 命令編譯當(dāng)前目錄中的包與測試沈条,然后運行生成的測試文件。在此模式下禁用緩存诅炉。在測試完成后蜡歹,go test 一行摘要顯示測試結(jié)果(“ok”或“FAIL”),包名涕烧,還有 已用時間月而。
第二種稱為包列表模式,go test 調(diào)用指定確切的包參數(shù)時發(fā)生(例如"go test math","go test ./..."甚至是"go test .")议纯。在此模式下父款,go test 編譯測試每個命令行中出現(xiàn)的包。如果包測試通過瞻凤,go test最后只會輸出一行“ok”作為總結(jié)憨攒。如果包測試失敗,go test 打印完整的測試輸出阀参。如果調(diào)用帶有 -bench 或 -v flag浓恶,go test 即使測試通過的包也會打印完整的輸出,為了顯示基準(zhǔn)請求結(jié)果或榮譽的日志结笨。
僅在包列表模式下,go test 會緩存成功運行測試的包,以避免沒必要的重復(fù)執(zhí)行炕吸。當(dāng)可以從緩存中恢復(fù)測試結(jié)果時伐憾,go test將重新顯示先前的輸出,而不是再次運行測試二進制赫模。發(fā)生這種情況時树肃,go test 在輸出行的使用時間后邊打印“(cached)”。
緩存匹配的規(guī)則是:運行相同的測試文件且命令行上的標(biāo)記全部來自'可緩存的'集合(-cpu, -list, -parallel, -run, -short, and -v)瀑罗。如果執(zhí)行的go test 命令行有任何測試或非測試標(biāo)記不在上述集合中胸嘴,則結(jié)果不可緩存。要關(guān)閉緩存斩祭,使用任何不在上述集合的測試標(biāo)記劣像。不使用緩存的慣用法是使用標(biāo)記 -count=1。 Tests that open files within the package's source root (usually $GOPATH) or that consult environment variables only match future runs in which the files and environment variables are unchanged. A cached test result is treated as executing in no time at all, so a successful package test result will be cached and reused regardless of -timeout setting.
除了構(gòu)建標(biāo)記之外摧玫,“go test”本身處理的標(biāo)記是:
-args
Pass the remainder of the command line (everything after -args)
to the test binary, uninterpreted and unchanged.
Because this flag consumes the remainder of the command line,
the package list (if present) must appear before this flag.
-c
Compile the test binary to pkg.test but do not run it
(where pkg is the last element of the package's import path).
The file name can be changed with the -o flag.
-exec xprog
Run the test binary using xprog. The behavior is the same as
in 'go run'. See 'go help run' for details.
-i
Install packages that are dependencies of the test.
Do not run the test.
-json
Convert test output to JSON suitable for automated processing.
See 'go doc test2json' for the encoding details.
-o file
Compile the test binary to the named file.
The test still runs (unless -c or -i is specified).
測試文件還接受控制測試執(zhí)行的標(biāo)記耳奕,這些標(biāo)記可以通過“go test”訪問。
有關(guān)構(gòu)建標(biāo)志的更多信息诬像,請參閱“go help build”屋群。有關(guān)指定包的更多信息,請參閱“go help packages”坏挠。另見:go build芍躏,go vet。
Run specified go tool
用法:
go tool [-n] command [args...]
tool 運行有參數(shù)指定的go工具命令降狠。如果沒有參數(shù)对竣,它輸出已知的工具列表。
-n 標(biāo)記指示tool 打印即將執(zhí)行的命令喊熟,但是不執(zhí)行它們柏肪。
有關(guān)每個工具命令的更多信息,請參閱“go doc cmd / <command>”芥牌。
Print Go version
Usage:
go version
Version prints the Go version, as reported by runtime.Version.
Report likely mistakes in packages(報告包中可能的錯誤)
用法:
go vet [-n] [-x] [-vettool prog] [build flags] [vet flags] [packages]
vet 在導(dǎo)入路徑命名包上運行 go vet 命令
For more about vet and its flags, see 'go doc cmd/vet'. For more about specifying packages, see 'go help packages'. For a list of checkers and their flags, see 'go tool vet help'. For details of a specific checker such as 'printf', see 'go tool vet help printf'.
-n 標(biāo)記輸出即將執(zhí)行的命令烦味。-x 輸出執(zhí)行的命令
-vettool=prog 標(biāo)記選擇替代的分析工具或使用其它工具。例如使用以下命令構(gòu)建運行“shadow”分析工具壁拉。
go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow
go vet -vettool=$(which shadow)
支持go vet 的 build 標(biāo)記包含哪些控制包解析和執(zhí)行的標(biāo)記谬俄,如-n, -x, -v, -tags, and -toolexec
Build Modes(構(gòu)建模式)
"go build" 和 "go install" 命令支持使用 -buildmode 參數(shù)指示要構(gòu)建的文件類型。當(dāng)前支持的值是:
-buildmode=archive
Build the listed non-main packages into .a files. Packages named
main are ignored.
-buildmode=c-archive
Build the listed main package, plus all packages it imports,
into a C archive file. The only callable symbols will be those
functions exported using a cgo //export comment. Requires
exactly one main package to be listed.
-buildmode=c-shared
Build the listed main package, plus all packages it imports,
into a C shared library. The only callable symbols will
be those functions exported using a cgo //export comment.
Requires exactly one main package to be listed.
-buildmode=default
Listed main packages are built into executables and listed
non-main packages are built into .a files (the default
behavior).
-buildmode=shared
Combine all the listed non-main packages into a single shared
library that will be used when building with the -linkshared
option. Packages named main are ignored.
-buildmode=exe
Build the listed main packages and everything they import into
executables. Packages not named main are ignored.
-buildmode=pie
Build the listed main packages and everything they import into
position independent executables (PIE). Packages not named
main are ignored.
-buildmode=plugin
Build the listed main packages, plus all packages that they
import, into a Go plugin. Packages not named main are ignored.
Calling between Go and C (go與c之間互相調(diào)用)
有兩種不同的方法可以在Go語言 與 C/C++之間互相調(diào)用
第一種是 cgo 工具弃理,它是Go發(fā)行版的一部分溃论。有關(guān)如何使用的信息查看cgo文檔(go doc cmd/cgo)
第二種是SWIG程序,它是語言之間通用的接口工具痘昌。有關(guān)SWIG信息钥勋,參閱 http://swig.org/ 運行g(shù)o build時炬转,任何擴展名為.swig的文件都將傳遞給SWIG。任何擴展名為.swigcxx的文件都將使用-c ++選項傳遞給SWIG算灸。
當(dāng)使用cgo或SWIG時扼劈,go build會將任何.c,.m菲驴,.s或.S文件傳遞給C編譯器荐吵,將任何.cc,.cpp赊瞬,.cxx文件傳遞給C ++編譯器先煎。可以設(shè)置CC或CXX環(huán)境變量以分別確定要使用的C或C ++編譯器巧涧。
Build and test caching(構(gòu)建和測試緩存)
go 命令緩存構(gòu)建結(jié)果以便在將來構(gòu)建中重用薯蝎。緩存數(shù)據(jù)的默認(rèn)地址是當(dāng)前操作系統(tǒng)的標(biāo)準(zhǔn)用戶緩存目錄中名為go-build 的子目錄。設(shè)置 GOCACHE 環(huán)境變量會覆蓋此默認(rèn)值褒侧,運行 go env GOCACHE 將打印當(dāng)前緩存目錄良风。
go 命令定期刪除那些最新未使用的緩存數(shù)據(jù),運行 "go clean -cache"刪除所有緩存數(shù)據(jù)闷供。
構(gòu)建緩存正確的考慮了對Go源文件烟央,編譯器,編譯選項等的更改:在一般情況下無須手動顯式清除緩存歪脏。然而構(gòu)建緩存不會檢測使用cgo導(dǎo)入的C庫的更改疑俭。如果你更改了C鏈接庫,則需要手動顯式清除緩存婿失,或者使用 -a 構(gòu)建標(biāo)記強制重新構(gòu)建依賴于更新的C鏈接庫的包钞艇。
go命令也緩存成功的包測試結(jié)果。有關(guān)詳細信息豪硅,請參閱“go help test”哩照。運行'go clean -testcache'會刪除所有緩存的測試結(jié)果(但不會緩存構(gòu)建結(jié)果)。
The GODEBUG environment variable can enable printing of debugging information about the state of the cache:
GODEBUG=gocacheverify=1 causes the go command to bypass the use of any cache entries and instead rebuild everything and check that the results match existing cache entries.
GODEBUG=gocachehash=1 causes the go command to print the inputs for all of the content hashes it uses to construct cache lookup keys. The output is voluminous but can be useful for debugging the cache.
GODEBUG=gocachetest=1 causes the go command to print details of its decisions about whether to reuse a cached test result.
Environment variables(環(huán)境變量)
go 命令以及工具的調(diào)用懒浮,需要檢查幾個不同的環(huán)境變量飘弧。對于其中許多,您可以通過運行'go env NAME'來查看系統(tǒng)的默認(rèn)值砚著,其中NAME是變量的名稱次伶。
通用環(huán)境變量:
GCCGO
The gccgo command to run for 'go build -compiler=gccgo'.
GOARCH
The architecture, or processor, for which to compile code.
Examples are amd64, 386, arm, ppc64.
GOBIN
The directory where 'go install' will install a command.
GOCACHE
The directory where the go command will store cached
information for reuse in future builds.
GOFLAGS
A space-separated list of -flag=value settings to apply
to go commands by default, when the given flag is known by
the current command. Flags listed on the command-line
are applied after this list and therefore override it.
GOOS
The operating system for which to compile code.
Examples are linux, darwin, windows, netbsd.
GOPATH
For more details see: 'go help gopath'.
GOPROXY
URL of Go module proxy. See 'go help goproxy'.
GORACE
Options for the race detector.
See https://golang.org/doc/articles/race_detector.html.
GOROOT
The root of the go tree.
GOTMPDIR
The directory where the go command will write
temporary source files, packages, and binaries.
GOFLAGS列表中的每個條目都必須是獨立標(biāo)志。由于條目是以空格分隔的稽穆,因此標(biāo)志值不得包含空格冠王。
與cgo一起使用的環(huán)境變量:
CC
The command to use to compile C code.
CGO_ENABLED
Whether the cgo command is supported. Either 0 or 1.
CGO_CFLAGS
Flags that cgo will pass to the compiler when compiling
C code.
CGO_CFLAGS_ALLOW
A regular expression specifying additional flags to allow
to appear in #cgo CFLAGS source code directives.
Does not apply to the CGO_CFLAGS environment variable.
CGO_CFLAGS_DISALLOW
A regular expression specifying flags that must be disallowed
from appearing in #cgo CFLAGS source code directives.
Does not apply to the CGO_CFLAGS environment variable.
CGO_CPPFLAGS, CGO_CPPFLAGS_ALLOW, CGO_CPPFLAGS_DISALLOW
Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
but for the C preprocessor.
CGO_CXXFLAGS, CGO_CXXFLAGS_ALLOW, CGO_CXXFLAGS_DISALLOW
Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
but for the C++ compiler.
CGO_FFLAGS, CGO_FFLAGS_ALLOW, CGO_FFLAGS_DISALLOW
Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
but for the Fortran compiler.
CGO_LDFLAGS, CGO_LDFLAGS_ALLOW, CGO_LDFLAGS_DISALLOW
Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
but for the linker.
CXX
The command to use to compile C++ code.
PKG_CONFIG
Path to pkg-config tool.
AR
The command to use to manipulate library archives when
building with the gccgo compiler.
The default is 'ar'.
體系結(jié)構(gòu)特定的環(huán)境變量:
GOARM
For GOARCH=arm, the ARM architecture for which to compile.
Valid values are 5, 6, 7.
GO386
For GOARCH=386, the floating point instruction set.
Valid values are 387, sse2.
GOMIPS
For GOARCH=mips{,le}, whether to use floating point instructions.
Valid values are hardfloat (default), softfloat.
GOMIPS64
For GOARCH=mips64{,le}, whether to use floating point instructions.
Valid values are hardfloat (default), softfloat.
專用環(huán)境變量:
GCCGOTOOLDIR
If set, where to find gccgo tools, such as cgo.
The default is based on how gccgo was configured.
GOROOT_FINAL
The root of the installed Go tree, when it is
installed in a location other than where it is built.
File names in stack traces are rewritten from GOROOT to
GOROOT_FINAL.
GO_EXTLINK_ENABLED
Whether the linker should use external linking mode
when using -linkmode=auto with code that uses cgo.
Set to 0 to disable external linking mode, 1 to enable it.
GIT_ALLOW_PROTOCOL
Defined by Git. A colon-separated list of schemes that are allowed to be used
with git fetch/clone. If set, any scheme not explicitly mentioned will be
considered insecure by 'go get'.
“go env”提供的其他信息,但未從環(huán)境中讀壬嘞狻:
GOEXE
The executable file name suffix (".exe" on Windows, "" on other systems).
GOHOSTARCH
The architecture (GOARCH) of the Go toolchain binaries.
GOHOSTOS
The operating system (GOOS) of the Go toolchain binaries.
GOMOD
The absolute path to the go.mod of the main module,
or the empty string if not using modules.
GOTOOLDIR
The directory where the go tools (compile, cover, doc, etc...) are installed.