温馨提示×

debian运行golang报错怎么解

小樊
39
2026-10-22 18:54:51
栏目: 编程语言

在 Debian 上运行 Go(Golang)程序时报错,原因可能有很多。下面我按常见错误类型给你分类说明,并给出对应的解决办法。你可以对照自己的报错信息来找方案。


一、命令找不到:command not found: go

原因

Go 没有安装,或者没有加入 PATH

解决

  1. 查看是否安装
which go
go version
  1. 没安装则安装(推荐官方二进制):
sudo apt remove golang-go golang -y
wget https://go.dev/dl/go1.22.5.linux-amd64.tar.gz
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf go1.22.5.linux-amd64.tar.gz
  1. 加入 PATH(~/.bashrc 或 ~/.zshrc)
export PATH=$PATH:/usr/local/go/bin
source ~/.bashrc

二、go: command not found 或 go: error

常见于:

  • 用的是 apt install golang
  • 版本太老(Debian 自带 Go 很旧)

✅ 建议用官方 Go,不要用 apt 的旧版本


三、运行 go run main.go 报错

1. package main is not in GOROOT

main.go:1:1: package main is not in GOROOT

✅ 解决:

go mod init myproject
go run main.go

2. cannot find package

cannot find package "xxx"

✅ 解决:

go mod tidy
go get xxx

四、permission denied

bash: ./main: Permission denied

✅ 解决:

chmod +x main
./main

或:

go run main.go

五、GLIBC_2.xx not found

version `GLIBC_2.28' not found

原因

Go 程序是在新系统编译的,Debian 太老

解决

  • 在 Debian 上重新编译
  • 或使用静态编译:
CGO_ENABLED=0 go build

六、exec: "gcc": executable file not found

CGO_ENABLED=1 requires gcc

✅ 解决:

sudo apt install gcc

或禁用 CGO:

CGO_ENABLED=0 go build

七、环境变量问题(推荐配置)

go env -w GOPROXY=https://proxy.golang.org,direct
go env -w GO111MODULE=on

八、如果你愿意,我可以更精准帮你

请把完整报错信息贴出来,例如:

  • 你运行的命令
  • 完整错误输出
  • Debian 版本(cat /etc/os-release)
  • Go 版本(go version)

我可以直接告诉你具体怎么改。

0 踩