go 常用命令

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

巩固学习最好的方法是通过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 <command>

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 <topic>" for more information about that topic.

2.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”。

2.1.1 go help environment

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

2.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/pkg@v1.2.3

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

go get example.com/mod@none

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

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

2.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标志。

2.4 go vet

报告包装中可能的错误

2.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包

2.6 go mod

2.7 go install

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: go