下面给你一份在 Debian 上配置 Go(Golang) 的常用做法,从「最简单」到「推荐方式」都有。
适合:只是临时用、不追求新版本
sudo apt update
sudo apt install golang-go
检查:
go version
⚠️ Debian 仓库里的 Go 通常落后官方 1~2 个版本。
适合:开发、生产、需要新版本
去官网看最新版:https://go.dev/dl/
cd /tmp
wget https://go.dev/dl/go1.22.5.linux-amd64.tar.gz
⚠️ 如果是 ARM 架构,换成
arm64
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf go1.22.5.linux-amd64.tar.gz
编辑 ~/.bashrc 或 ~/.profile:
nano ~/.bashrc
添加:
export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
生效:
source ~/.bashrc
go version
适合:需要切换 Go 版本
bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
使用:
gvm install go1.22.5
gvm use go1.22.5 --default
mkdir ~/hello && cd ~/hello
go mod init hello
hello.go:
package main
import "fmt"
func main() {
fmt.Println("Hello, Debian + Go!")
}
运行:
go run hello.go
go: command not found说明 PATH 没生效,确认:
which go
或重新 source ~/.bashrc
可设置代理:
go env -w GOPROXY=https://goproxy.cn,direct
把 PATH 加到 /etc/profile.d/go.sh
如果你告诉我:
我可以给你更精确的配置方案。