vscode
git clone https://github.com/golang/go
cd golang/go
编译的主要入口是src/make.bash
(windows则是src/make.bat
):
# src/make.bash
# GOROOT_FINAL: The expected final Go root, baked into binaries.
# The default is the location of the Go tree during the build.
#
# GOROOT_BOOTSTRAP: A working Go tree >= Go 1.4 for bootstrap.
# If $GOROOT_BOOTSTRAP/bin/go is missing, $(go env GOROOT) is
# tried for all "go" in $PATH. $HOME/go1.4 by default.
if [ ! -f run.bash ]; then
echo 'make.bash must be run from $GOROOT/src' 1>&2
exit 1
fi
...
有两个环境变量值得注意:
GOROOT_FINAL
最终输出的编译的go的目录GOROOT_BOOTSTRAP
用于编译go的基础go版本,必须>=go1.4
必须在src
目录运行make.bash
。
cd src
rm -rf build_go && mkdir -p build_go
GOROOT_FINAL=build_go ./make.bash
日志:
Building Go cmd/dist using /Users/xhd2015/installed/go1.17. (go1.17.5 darwin/amd64)
Building Go toolchain1 using /Users/xhd2015/installed/go1.17.
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 darwin/amd64.
---
Installed Go for darwin/amd64 in /Users/xhd2015/Projects/gopath/src/github.com/golang/go
Installed commands in /Users/xhd2015/Projects/gopath/src/github.com/golang/go/bin
The binaries expect /Users/xhd2015/Projects/gopath/src/github.com/golang/go to be copied or moved to build_go
运行编译完成的go
;
$ GOROOT=$PWD bin/go version
go version devel go1.19-416c953960 Sun Jun 26 00:26:59 2022 +0000 darwin/amd64
虽然src/cmd/go能够编译成go
,但是如果直接使用系统的工具编译:
cd src/cmd
go build -o cmd_go ./go
./cmd_go # 这里实际是以系统的源码进行编译的
必须使用src/make.bash
才能编译出当前目录的go
。
GOROOT=../.. ../../bin/go build -gcflags=all="-N -l" -o /tmp/go/cmd/cmd_go.bin ./go
vscode需要设置go.goroot
:
"go.goroot": "/Users/xhd2015/Projects/gopath/src/github.com/golang/go",
并重启。这样才能解析到对应的符号。