golang的后台编译流程

先了解编译相关的基本概念

环境变量相关

仅列举一些常用的,其他的环境变量可以通过 go env 命令 来查看

1、GOROOT

go语言的安装目录,方便我们找到所安装的SDK

2、GOPATH

go的工作源码目录,也就是项目代码文件的存放目录。
可以写多个值,查找的时候根据顺序进行查找。比如 项目中配置的包的路径一般是相对于项目而言的。
比如 imp的包路径 /laozhao/motuo/baoma。对于项目而言,项目在本地磁盘的路径是D:/a/b/laozhao/… ;这时,必须配置GOPATH的路径是D:/a/b/才能正确导包

也可以不设置,在工作目录和安装目录相同的情况下(默认安装)

3、GOOS

go所在的操作系统类型

比如在windows中 我们输入 go env GOOS
在这里插入图片描述

4、GOARCH

go所在的计算机架构

在windows中我们查看下,输入 go env GOARCH
在这里插入图片描述

5、GOBIN

  • 程序生成的可执行文件的路径
  • 不是必须设置的
  • 默认是空

go 编译过程

1、go run

编译和运行合并为一步

只能运行 .go 结尾的 go 源码文件,且不能运行测试源码文件

-n 参数可打印出具体的执行过程

C:\Users\zhaozheng\go\src\awesomeProject>go run -n test.go
mkdir -p $WORK\b001\
cat >$WORK\b001\importcfg.link << 'EOF' # internal
packagefile command-line-arguments=C:\Users\zhaozheng\AppData\Local\go-build\8e\8e76b279632afa8ef8bf213c2ad4d1fa25c3dbce2dc039a54e42367ada8db7d1-d
packagefile fmt=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\fmt.a
packagefile runtime=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\runtime.a
packagefile errors=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\errors.a
packagefile internal/fmtsort=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\fmtsort.a
packagefile io=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\io.a
packagefile math=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\math.a
packagefile os=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\os.a
packagefile reflect=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\reflect.a
packagefile strconv=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\strconv.a
packagefile sync=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\sync.a
packagefile unicode/utf8=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\unicode\utf8.a
packagefile internal/bytealg=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\bytealg.a
packagefile internal/cpu=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\cpu.a
packagefile runtime/internal/atomic=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\runtime\internal\atomic.a
packagefile runtime/internal/math=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\runtime\internal\math.a
packagefile runtime/internal/sys=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\runtime\internal\sys.a
packagefile internal/reflectlite=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\reflectlite.a
packagefile sort=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\sort.a
packagefile math/bits=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\math\bits.a
packagefile internal/oserror=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\oserror.a
packagefile internal/poll=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\poll.a
packagefile internal/syscall/execenv=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\syscall\execenv.a
packagefile internal/syscall/windows=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\syscall\windows.a
packagefile internal/testlog=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\testlog.a
packagefile sync/atomic=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\sync\atomic.a
packagefile syscall=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\syscall.a
packagefile time=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\time.a
packagefile unicode/utf16=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\unicode\utf16.a
packagefile internal/unsafeheader=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\unsafeheader.a
packagefile unicode=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\unicode.a
packagefile internal/race=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\race.a
packagefile internal/syscall/windows/sysdll=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\syscall\windows\sysdll.a
packagefile internal/syscall/windows/registry=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\syscall\windows\registry.a
EOF
mkdir -p $WORK\b001\exe\
cd .
"D:\\soft\\go1.15.8.windows-amd64\\go\\pkg\\tool\\windows_amd64\\link.exe" -o "$WORK\\b001\\exe\\test.exe" -importcfg "$WORK\\b001\\importcfg.link" -s -w -buildmode=pie
-buildid=EdaQr_LJbZnMVwrbTfVL/I-WJOY6riYvsxqAm3cS0/GGCfvdysfia70eprwMzD/EdaQr_LJbZnMVwrbTfVL -extld=gcc "C:\\Users\\zhaozheng\\AppData\\Local\\go-build\\8e\\8e76b279632a
fa8ef8bf213c2ad4d1fa25c3dbce2dc039a54e42367ada8db7d1-d"
$WORK\b001\exe\test.exe
  • 首先创建两个文件夹 b001 和 exe。最终的可执行文件会放在exe文件夹里,最后执行这个文件。
  • 利用go run -work 可以观察临时文件的路径

总结

go run 编译并直接运行程序,它会产生一个临时文件(但不会生成 .exe 文件),直接在命令行输出程序执行结果,方便用户调试

2、go build

  • 编译源码文件为可执行文件,会在当前编译目录下生成一个编译文件同名的exe文件
  • 编译检查源码文件的有效性,做检查性的编译
  • go build 后面不跟文件名的话,默认编译当前文件下所有的可执行文件(.go)
  • go build 后面加包名,会将该包下所有的可执行文件编译成一个以包名命名的可执行文件
  • go build 其他参数
    在这里插入图片描述

-race 可以提前观察到 程序中是否存在并发情况,比较重要,作为我们自测的一个方法

看下go build 都做了什么事情

C:\Users\zhaozheng\go\src\awesomeProject>go build -n test.go

#
# command-line-arguments
#

mkdir -p $WORK\b001\
cat >$WORK\b001\importcfg << 'EOF' # internal
# import config
packagefile fmt=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\fmt.a
packagefile runtime=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\runtime.a
EOF
cd C:\Users\zhaozheng\go\src\awesomeProject
"D:\\soft\\go1.15.8.windows-amd64\\go\\pkg\\tool\\windows_amd64\\compile.exe" -o "$WORK\\b001\\_pkg_.a" -trimpath "$WORK\\b001=>" -p main -complete -buildid AZKJUGyKz6QW
17fu6SDa/AZKJUGyKz6QW17fu6SDa -goversion go1.15.8 -D _/C_/Users/zhaozheng/go/src/awesomeProject -importcfg "$WORK\\b001\\importcfg" -pack -c=4 "C:\\Users\\zhaozheng\\go\
\src\\awesomeProject\\test.go"
"D:\\soft\\go1.15.8.windows-amd64\\go\\pkg\\tool\\windows_amd64\\buildid.exe" -w "$WORK\\b001\\_pkg_.a" # internal
cat >$WORK\b001\importcfg.link << 'EOF' # internal
packagefile command-line-arguments=$WORK\b001\_pkg_.a
packagefile fmt=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\fmt.a
packagefile runtime=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\runtime.a
packagefile errors=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\errors.a
packagefile internal/fmtsort=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\fmtsort.a
packagefile io=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\io.a
packagefile math=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\math.a
packagefile os=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\os.a
packagefile reflect=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\reflect.a
packagefile strconv=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\strconv.a
packagefile sync=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\sync.a
packagefile unicode/utf8=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\unicode\utf8.a
packagefile internal/bytealg=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\bytealg.a
packagefile internal/cpu=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\cpu.a
packagefile runtime/internal/atomic=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\runtime\internal\atomic.a
packagefile runtime/internal/math=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\runtime\internal\math.a
packagefile runtime/internal/sys=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\runtime\internal\sys.a
packagefile internal/reflectlite=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\reflectlite.a
packagefile sort=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\sort.a
packagefile math/bits=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\math\bits.a
packagefile internal/oserror=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\oserror.a
packagefile internal/poll=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\poll.a
packagefile internal/syscall/execenv=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\syscall\execenv.a
packagefile internal/syscall/windows=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\syscall\windows.a
packagefile internal/testlog=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\testlog.a
packagefile sync/atomic=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\sync\atomic.a
packagefile syscall=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\syscall.a
packagefile time=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\time.a
packagefile unicode/utf16=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\unicode\utf16.a
packagefile internal/unsafeheader=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\unsafeheader.a
packagefile unicode=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\unicode.a
packagefile internal/race=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\race.a
packagefile internal/syscall/windows/sysdll=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\syscall\windows\sysdll.a
packagefile internal/syscall/windows/registry=D:\soft\go1.15.8.windows-amd64\go\pkg\windows_amd64\internal\syscall\windows\registry.a
EOF
mkdir -p $WORK\b001\exe\
cd .
"D:\\soft\\go1.15.8.windows-amd64\\go\\pkg\\tool\\windows_amd64\\link.exe" -o "$WORK\\b001\\exe\\a.out.exe" -importcfg "$WORK\\b001\\importcfg.link" -buildmode=pie -buil
did=IxL2d31A6uWKiOp8De8b/AZKJUGyKz6QW17fu6SDa/AZKJUGyKz6QW17fu6SDa/IxL2d31A6uWKiOp8De8b -extld=gcc "$WORK\\b001\\_pkg_.a"
"D:\\soft\\go1.15.8.windows-amd64\\go\\pkg\\tool\\windows_amd64\\buildid.exe" -w "$WORK\\b001\\exe\\a.out.exe" # internal
mv $WORK\b001\exe\a.out.exe test.exe

和go run 的过程基本相同,只是 go run 在 exe 文件夹创建可执行文件后会运行,但是 go build 会把这个可执行文件移动到当前文件夹,不执行

3、go install

  • 于go build 相同都能生成可执行文件,但是 go install 可以编译生成 包文件
  • go install 是建立在 GOPATH 上的,无法在独立的目录使用 go install 命令
  • go install 的输出目录始终是 GOPATH 下的bin目录。可执行文件 引入的包会被编译到 $GOPATH/pkg/ 目录下

4、go get

  • 借助代码管理工具 从远程拉取或更新代码及依赖包
  • 这个命令可以动态获取远程代码包,目前支持的有 BitBucket、GitHub、Google Code 和 Launchpad。在使用 go get 命令前,需要安装与远程包匹配的代码管理工具,如 Git、SVN、HG 等,参数中需要提供一个包名。
  • 这个命令在内部实际上分成了两步操作:第一步是下载源码包,第二步是执行 go install
  • 参数介绍
    -d 只下载不安装
    -f 只有在你包含了 -u 参数的时候才有效,不让 -u 去验证 import 中的每一个都已经获取了,这对于本地 fork 的包特别有用
    -fix 在获取源码之后先运行 fix,然后再去做其他的事情
    -t 同时也下载需要为运行测试所需要的包
    -u 强制使用网络去更新包和它的依赖包
    -v 显示执行的命令
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值