下面按常见场景给你讲怎么在 Debian 上安装 Go(Golang)。
适合:只是想用 Go,不追求最新版本
sudo apt update
sudo apt install golang-go
验证:
go version
注意:Debian 稳定版里的 Go 通常落后官方一两个大版本。
先去 https://go.dev/dl/ 查看最新版本
cd /tmp
wget https://go.dev/dl/go1.22.5.linux-amd64.tar.gz
/usr/localsudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf go1.22.5.linux-amd64.tar.gz
编辑 ~/.bashrc(或 ~/.zshrc):
nano ~/.bashrc
添加:
export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
使配置生效:
source ~/.bashrc
go version
goenv(类似 pyenv)git clone https://github.com/syndbg/goenv.git ~/.goenv
配置环境变量后:
goenv install 1.22.5
goenv global 1.22.5
g(轻量级)bash < <(curl -sSL https://git.io/g-install)
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 没配置好,检查 .bashrc
可换代理:
go env -w GOPROXY=https://goproxy.cn,direct
如果你告诉我:
我可以给你更精确的安装方案。