在Debian编译Golang时遇到错误,可按以下步骤排查解决:
检查Go安装与环境变量
go version 确认安装正确,未安装则用 sudo apt update && sudo apt install golang-go 安装。GOROOT(默认 /usr/lib/go)、GOPATH(默认 $HOME/go)是否正确配置,添加到 ~/.bashrc 并执行 source 生效。处理依赖问题
go get 安装缺失包(如 go get github.com/example/package)。go mod tidy 整理依赖(需先初始化模块 go mod init)。排查代码与编译选项
CGO_ENABLED=0 及目标平台参数(如 GOOS=linux GOARCH=amd64)。清理缓存与查看详细日志
go clean -cache 清理编译缓存后重新编译。go build -x 查看详细编译过程,定位错误环节。其他高级操作
GOPROXY=https://goproxy.cn,direct。