go 常用命令

巩固学习最好的方法是通过go help看文档

GO语言规范文档

终端执行命令 go help environment
GOBIN
The directory where ‘go install’ will install a command.

go 命令使用

go <command> [arguments]

command:

The commands are:

	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         向当前模块添加依赖项并安装它们
	
	install     编译和安装包和依赖项
	list        list packages or modules
	mod         module maintenance
	work        workspace 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

topics:

Additional help topics:

	buildconstraint build constraints
	buildmode       build modes
	c               calling between Go and C
	cache           build and test caching
	environment     environment variables
	filetype        file types
	go.mod          the go.mod file
	gopath          GOPATH environment variable
	gopath-get      legacy GOPATH go get
	goproxy         module proxy protocol
	importpath      import path syntax
	modules         modules, module versions, and more
	module-get      module-aware go get
	module-auth     module authentication using go.sum
	packages        package lists and patterns
	private         configuration for downloading non-public code
	testflag        testing flags
	testfunc        testing functions
	vcs             controlling version control with GOVCS

Use "go help " for more information about that topic.

1 go env

go env [-json] [-u] [-w] [var ...]

env打印Go环境信息。

默认情况下,env以shell脚本(在Windows上是批处理文件)的形式打印信息。如果给出了一个或多个变量名作为参数,env将每个命名变量的值打印在它自己的行上。

-json 以JSON格式而不是shell脚本打印环境。
-u 需要一个或多个参数,如果已使用’go env -w’设置了命名环境变量,则该标志将取消(unset)指定环境变量为默认设置。
-w 需要一个或多个NAME=VALUE形式的参数,并将已命名环境变量的默认设置更改为给定值。

有关环境变量的更多信息,请参见“go help environment”。

1.1 go help environment

go命令及其调用的工具会参考环境变量进行配置。

如果一个环境变量没有设置,go命令会使用合理的默认设置。

要查看变量的有效设置,请运行go env

执行go env -w =命令可更改默认设置。

使用go env -w默认值的更改将记录在存储在每个用户配置目录中的go环境配置文件中,如os.UserConfigDir所报告的。

配置文件的位置可以通过设置环境变量GOENV来改变,go env GOENV打印有效位置,但go env -w不能改变默认位置。
详见go help env

在这里插入图片描述

通用环境变量:

GOBIN
“go install” 安装命令的目录。

GOROOT
The root of the go tree.

GOENV
Go环境配置文件的位置。不能使用’go env -w’设置。
在环境中设置GOENV=off将禁用默认配置文件。

用于cgo的环境变量:

特定于体系结构的环境变量:

特殊用途的环境变量:

可从’go env’获取但不能从environment读取的其他信息:

2 go get

go get [-t] [-u] [-v] [build flags] [packages]

Get将其命令行参数解析为特定模块版本的包,更新go.mod来要求这些版本,并将源代码下载到模块缓存中。然后构建并安装命名的包。

“go get”用于调整go.mod中的依赖项,“go install”可以用来构建和安装命令。当指定了一个版本时,'go install’将以 module-aware模式运行,并忽略在当前目录的go.mod文件

为一个包添加依赖项或将其升级到最新版本

go get example.com/pkg

升级或降级一个包到特定的版本

go get example.com/[email protected]

移除对某个模块的依赖,并降级模块对它的依赖

go get example.com/mod@none

除了构建标志(build flags)(列在“go help build”中),go get接受以下标志。

-t 标志指示 get 考虑构建测试所需的模块
-u 标志指示get当有更新的次要或补丁版本可用时,更新模块

3 go test

go test [build/test flags] [packages_import_path] [build/test flags & test binary flags]

Go test自动测试导入路径下的包

“Go test”将重新编译每个包以及文件名与“*_test.go”匹配的任何文件。

这些附加文件可以包含测试函数、基准测试函数、模糊测试和示例函数。参见’go help testfunc’

每个列出的包导致执行一个单独的测试二进制文件。
文件名以“_”(包括“_test.go”)或“.”开头的文件将被忽略。

声明带有后缀“_test”的包的测试文件将被编译为一个单独的包,然后与主测试二进制文件链接并运行。

go工具将忽略名为“testdata”的目录,使其可用于保存测试所需的辅助数据

要禁用go vet的运行,使用-vet=off标志。要运行所有检查,使用-vet=all标志。

4 go vet

报告包装中可能的错误

5 go build

编译包和依赖项

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.

When compiling packages, build ignores files that end in ‘_test.go’

-tags tag,list
以逗号分隔的构建标记列表,以在构建期间考虑是否满足。参见go/build包

6 go mod

7 go install

你可能感兴趣的:(Golang,golang,linux,windows)