go 常用命令

本文详细介绍了Go语言的命令行工具,包括`gobuild`,`goenv`,`goget`,`gotest`等,以及环境变量的设置和模块管理。此外,还涵盖了如何使用`gohelp`获取更多关于特定命令和话题的帮助信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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命令会使用合理的默认设置。

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

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

使用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]

-u

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更新命令行中向包提供依赖的模块,以便在可用时使用更新的minor或补丁版本。

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当有更新的次要或补丁版本可用时,更新模块

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’

go里面没有明确的区分debug和release,不过你可以在编译的时候把一些debug信息给去掉:

go build -ldflags "-s"
-s: omit symbol table and debug info(忽略符号表和debug信息)

ldflags 可以设置链接选项

-w 不生成 DWARF 调试信息
-s 关闭符号表

-w-s 通常一起使用,用来减少可执行文件的体积。但删除了调试信息后,可执行文件将无法使用 gdb/dlv 调试

-tags tag,list
一个逗号分隔的附加构建标记列表,在构建过程中考虑是否满足。有关构建标签的更多信息,请参见“go help buildconstraint”。(早期版本的Go使用空格分隔列表,这种形式已被弃用,但仍然可以识别。)

go help buildconstraint

构建约束( build constraint),也称为构建标记(build tag),是一个文件应该包含在包中的条件。构建约束由开始的行注释给出

	//go:build

约束可以出现在任何类型的源文件中(不只是Go),但它们必须出现在文件的顶部附近,前面只能有空行和其他行注释。这些规则意味着在Go文件中,构建约束必须出现在package子句之前。

6 go mod

Go mod提供了对模块操作的访问。

注意,所有的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
	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重置主模块的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.

7 go install

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值