前言
命令行工具 go 相关的代码在 <go-src>/src/cmd/go,目录结构
|
|
main.go,入口函数
*_test.go,单元测试代码
internal,go 内部实现相关代码
internal 目录下基本上按照 go “子命令” 进行组织,可以看到常用的子命令比如 help, list, run .etc
|
|
main.go
main.go 是 go 命令的入口,为了优雅的支持各种 “子命令”,main.go 将各个子命令对象保存在数组里,通过遍历数组找到具体的子命令,然后调用各个子命令的 run 方法
|
|
Command struct
A command is an implementation of a go command
|
|
go 支持的所有 Command 存放在 base package 的 Command 数组 Command,并在 main.go 的 init 函数中初始化
|
|
go build
build command 的相关代码在 <go-src>/src/cmd/go/internal/work/build.go,在讨论具体的流程之前,我们先来看一些和 build 相关的数据结构
toolchain interface
toolchain 接口定义了在编译过程中一些通用的方法
|
|
noToolchain
goToolchain
gccgoToolchain
Action
An action represents a single action in the action graph
Action 结构封装了 build 过程中的一个阶段性的”动作”,Deps 字段描述它依赖哪些 Action,triggers 字段描述哪些 Action 依赖它,通过这两个字段,所有的 Action 组成一个 action graphic
|
|
Builder
A Builder holds global state about a build. It does not hold per-package state, because we build packages in parallel, and the builder is shared.
|
|