gopl-zh.github.com/ch10/ch10-07.md

2.3 KiB
Raw Blame History

10.7. 工具

本章剩下的部分将讨论Go语言工具箱的具体功能包括如何下载、格式化、构建、测试和安装Go语言编写的程序。

Go语言的工具箱集合了一系列的功能的命令集。它可以看作是一个包管理器类似于Linux中的apt和rpm工具用于包的查询、计算的包依赖关系、从远程版本控制系统和下载它们等任务。它也是一个构建系统计算文件的依赖关系然后调用编译器、汇编器和连接器构建程序虽然它故意被设计成没有标准的make命令那么复杂。它也是一个单元测试和基准测试的驱动程序我们将在第11章讨论测试话题。

Go语言工具箱的命令有着类似“瑞士军刀”的风格带着一打子的子命令有一些我们经常用到例如get、run、build和fmt等。你可以运行go或go help命令查看内置的帮助文档为了查询方便我们列出了最常用的命令

$ go
...
	build            compile packages and dependencies
	clean            remove object files
	doc              show documentation for package or symbol
	env              print Go environment information
	fmt              run gofmt on package sources
	get              download and install packages and dependencies
	install          compile and install packages and dependencies
	list             list packages
	run              compile and run Go program
	test             test packages
	version          print Go version
	vet              run go tool vet on packages

Use "go help [command]" for more information about a command.
...

为了达到零配置的设计目标Go语言的工具箱很多地方都依赖各种约定。例如根据给定的源文件的名称Go语言的工具可以找到源文件对应的包因为每个目录只包含了单一的包并且到的导入路径和工作区的目录结构是对应的。给定一个包的导入路径Go语言的工具可以找到对应的目录中没个实体对应的源文件。它还可以根据导入路径找到存储代码仓库的远程服务器的URL。

{% include "./ch10-07-1.md" %}

{% include "./ch10-07-2.md" %}

{% include "./ch10-07-3.md" %}

{% include "./ch10-07-4.md" %}

{% include "./ch10-07-5.md" %}

{% include "./ch10-07-6.md" %}