使用Golang从源码运行go install命令的方法
使用Golang从源码运行go install命令的方法 大家好,
我按照官方指南 go.dev/doc/install/source 从源码编译 Go 后遇到了一个问题。虽然初始编译和测试看起来是成功的,但尝试使用 go install 安装任何包都会导致“写入屏障被禁止”的错误。
尽管进行了大量研究,我还没有找到任何针对从源码构建 Go 时出现此错误的解决方案。有趣的是,使用预构建的 Go 二进制文件可以避免这个问题。这让我怀疑是我的源码编译过程出了错,可能是在执行 all.bash 期间。
有其他人遇到过这个问题吗?任何指导都将不胜感激,因为我非常希望为了学习而理解和解决这个问题。
供参考,以下是错误信息:
../../../../../oss/go/src/runtime/mheap.go:537:8: write barrier prohibited
../../../../../oss/go/src/runtime/mgcwork.go:125:20: write barrier prohibited
../../../../../oss/go/src/runtime/mgcwork.go:125:20: write barrier prohibited by caller; (*gcWork).put
../../../../../oss/go/src/runtime/mgcwork.go:183:21: write barrier prohibited
../../../../../oss/go/src/runtime/mgcwork.go:183:21: write barrier prohibited by caller; (*gcWork).putBatch
../../../../../oss/go/src/runtime/mgcwork.go:212:20: write barrier prohibited
../../../../../oss/go/src/runtime/mgcwork.go:259:11: write barrier prohibited
../../../../../oss/go/src/runtime/mgcwork.go:295:11: write barrier prohibited
../../../../../oss/go/src/runtime/debuglog.go:102:14: write barrier prohibited
../../../../../oss/go/src/runtime/debuglog.go:102:14: write barrier prohibited by caller; dlogImpl
../../../../../oss/go/src/runtime/mgcwork.go:183:21: too many errors
以下是 go env 的输出:
GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='[home path]/Library/Caches/go-build'
GOENV='[home path]/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='[home path]/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='[home path]/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='[git repo path]/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='[git repo path]/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.2'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='[home path]/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/vx/r97ggjts11n2lvn3hgl5nbsh0000gn/T/go-build2015904587=/tmp/go-build -gno-record-gcc-switches -fno-common'
更多关于使用Golang从源码运行go install命令的方法的实战教程也可以访问 https://www.itying.com/category-94-b0.html
看起来我通过从源代码重新编译成功解决了这个问题。我怀疑,虽然没有确凿的证据,但原因可能是由于将 macOS (Sequoia 15.0.1) 升级到新版本导致了这些错误。
更多关于使用Golang从源码运行go install命令的方法的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
从源码编译Go时遇到“write barrier prohibited”错误通常是由于编译器或运行时的不一致造成的。以下是解决此问题的具体步骤:
1. 清理并重新构建工具链
cd /path/to/go/src
rm -rf ../pkg ../bin
./all.bash
2. 验证构建环境 确保没有残留的旧版本文件:
# 检查GOROOT设置是否正确
echo $GOROOT
# 应该是你的源码路径,如:/path/to/go
# 清理go缓存
go clean -cache -modcache
3. 使用正确的引导版本 如果你是从旧版本升级,可能需要使用Go 1.4作为引导:
# 下载Go 1.4引导编译器
cd /path/to
git clone https://go.googlesource.com/go goroot-bootstrap
cd goroot-bootstrap
git checkout go1.4-bootstrap-20171003
cd src
./make.bash
# 使用引导编译器构建新版本
cd /path/to/go/src
GOROOT_BOOTSTRAP=/path/to/goroot-bootstrap ./all.bash
4. 检查源码完整性 确保源码树完整且没有损坏:
cd /path/to/go
git status
git clean -fdx
git reset --hard
5. 使用dist工具重新构建
cd /path/to/go/src
./make.bash --dist-tool
# 然后使用新构建的工具链
../bin/go install your-package
6. 如果问题仍然存在,尝试最小化复现 创建一个简单的测试程序:
// test_install.go
package main
import "fmt"
func main() {
fmt.Println("Testing go install")
}
然后运行:
cd /tmp
../go/bin/go install test_install.go
这个错误通常表明运行时和编译器之间的版本不匹配。通过完全清理和重新构建工具链,大多数情况下可以解决这个问题。确保在构建过程中没有使用任何预编译的二进制文件,所有组件都从源码重新构建。

