在 CentOS 上快速配置 Golang 开发环境,推荐用 官方二进制包,简单、稳定、可控。下面给你一套 常用且最快 的做法(适用于 CentOS 7 / 8 / Stream)。
sudo yum remove golang -y
cd /usr/local
sudo wget https://go.dev/dl/go1.22.4.linux-amd64.tar.gz
如需其他版本,可访问:https://go.dev/dl/
sudo tar -C /usr/local -xzf go1.22.4.linux-amd64.tar.gz
编辑配置文件:
sudo vim /etc/profile
在文件末尾添加:
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
立即生效:
source /etc/profile
✅ 推荐额外配置(国内开发者):
go env -w GOPROXY=https://goproxy.cn,direct
go env -w GO111MODULE=on
go version
输出示例:
go version go1.22.4 linux/amd64
mkdir -p ~/hello && cd ~/hello
go mod init hello
创建 main.go:
package main
import "fmt"
func main() {
fmt.Println("Hello Golang on CentOS!")
}
运行:
go run main.go
⚠️ 版本可能较旧,不建议生产环境使用:
sudo dnf install golang -y
go version
go: command not found检查:
which go
echo $PATH
go env -w GOPROXY=https://goproxy.cn,direct
root 开发sudosudo yum install -y git vim
或用 VS Code + Remote SSH 连 CentOS 开发。
如果你需要:
告诉我你的 CentOS 版本 + 使用场景,我可以帮你定制一套方案。