在 Windows 上安装 golang 后,go 自身已经具备完整的 toolchain 即编译工具链。原生的 go 需要 C 编译器,但在 Windows 上我们可以使用 go 来编译自身。
首先我们要找到 go 的源码托管在哪儿,难道 go 不是托管在 github 上吗?我们在 go 的 github 项目首页可以看到,这只是一个镜像库,go 的代码库托管在它自己的源码网站上。
# github 镜像库https://github.com/golang/go# 原始库https://go.googlesource.com/go
首先下载 go 的源码,并 checkout 出一个指定版本:
$ git clone https://go.googlesource.com/go$ git tag$ git checkout -b go1.20rc3 go1.20rc3$ git branch* go1.20rc3master
Windows 的编译脚本是 go/src 目录,执行 all.bat 即可进行编译:
C:\> cd d d:\gitRepo\goC:\> cd srcd:\gitRepo\go\src> all.batERROR: Cannot find C:\Users\hyang0\Go1.4\bin\go.exeSet GOROOT_BOOTSTRAP to a working Go tree >= Go 1.17.13.
编译时会报错,提示需要设置 GOROOT_BOOTSTRAP 变量。我们使用本机的 go 的 GOROOT 作为这个变量的内容。
C:\> go env | findstr GOROOTset GOROOT=C:\Program Files\GoC:\> set GOROOT_BOOTSTRAP=C:\Program Files\GoC:\> echo %GOROOT_BOOTSTRAP%C:\Program Files\God:\gitRepo\go\src> all.batBuilding Go cmd/dist using C:\Program Files\Go. (go1.20.2 windows/amd64)go tool dist: cannot invoke C compiler ["gcc"]: exec: "gcc": executable file not found in %PATH%Go needs a system C compiler for use with cgo.To set a C compiler, set CC=the-compiler.To disable cgo, set CGO_ENABLED=0.
因为 Windows 上没有安装 C 编译器,所以要禁用 CGO_ENABLED 选项。
d:\gitRepo\go\src>set CGO_ENABLED=0d:\gitRepo\go\src>all.batBuilding Go cmd/dist using C:\Program Files\Go. (go1.20.2 windows/amd64)Building Go toolchain1 using C:\Program Files\Go.Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.Building Go toolchain2 using go_bootstrap and Go toolchain1.Building Go toolchain3 using go_bootstrap and Go toolchain2.Building packages and commands for windows/amd64.
随后编译脚本开始对环境进行测试:
##### Test execution environment.# GOARCH: amd64# CPU: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz# GOOS: windows# OS Version: 10.0.19044##### Testing packages.ok archive/tar 1.482sok archive/zip 1.481sok bufio 1.374sok bytes 1.583sok compress/bzip2 1.458sok compress/flate 2.676sok compress/gzip 5.540sok compress/lzw 4.312sok compress/zlib 2.508sok container/heap 1.119sok container/list 4.312sok container/ring 4.308s
测试项如果不通过会停止编译,编译过程会有点长。
还有一种替代方案是使用 gotip 编译:
C:\> go install golang.org/dl/gotip@latestC:\> gotip downloadCloning into 'C:\Users\hyang0\sdk\gotip'...remote: Finding sources: 100% (13139/13139)remote: Total 13139 (delta 1504), reused 8121 (delta 1504)Receiving objects: 100% (13139/13139), 26.97 MiB | 11.20 MiB/s, done.Resolving deltas: 100% (1504/1504), done.Updating files: 100% (12093/12093), done.Updating the go development tree...From https://go.googlesource.com/go* branch master -> FETCH_HEADHEAD is now at 70f98a2 errors: add ErrUnsupportedBuilding Go cmd/dist using C:\Program Files\Go. (go1.20.2 windows/amd64)go tool dist: cannot invoke C compiler ["gcc"]: exec: "gcc": executable file not found in %PATH%Go needs a system C compiler for use with cgo.To set a C compiler, set CC=the-compiler.To disable cgo, set CGO_ENABLED=0.Success. You may now run 'gotip'!C:\> gotip versiongo version devel go1.21-70f98a2 Sat Mar 11 05:07:02 2023 +0000 windows/amd64
编译完的 gotip 就是最新版本的 go ,可以执行 version 命令查看其版本号及编译时间。如果需要重新编译可以再次执行 gotip download 命令:
C:\> gotip downloadUpdating the go development tree...From https://go.googlesource.com/go* branch master -> FETCH_HEADHEAD is now at 70f98a2 errors: add ErrUnsupportedBuilding Go cmd/dist using C:\Program Files\Go. (go1.20.2 windows/amd64)Building Go toolchain1 using C:\Program Files\Go.Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.Building Go toolchain2 using go_bootstrap and Go toolchain1.Building Go toolchain3 using go_bootstrap and Go toolchain2.Building packages and commands for windows/amd64.---Installed Go for windows/amd64 in C:\Users\hyang0\sdk\gotipInstalled commands in C:\Users\hyang0\sdk\gotip\binSuccess. You may now run 'gotip'!C:\>
我本机的环境可能有点问题,基于源码的编译在环境检查阶段就失败了,晚点换个环境再试试。但基于 gotip 的能正常编译,而且速度很快。
参考
https://go.dev/doc/install/source
全文完。
如果转发本文,文末务必注明:“转自微信公众号:生有可恋”。
文章转载自生有可恋,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




