在 CentOS 下配置 Golang(Go 语言环境),常见做法有两种:官方二进制安装(推荐) 和 yum 安装(版本可能较旧)。下面给你最实用、最通用的步骤。
sudo rm -rf /usr/local/go
先查看最新版本:https://go.dev/dl/
cd /tmp
wget https://go.dev/dl/go1.22.4.linux-amd64.tar.gz
如果是 ARM 架构(如 aarch64):
wget https://go.dev/dl/go1.22.4.linux-arm64.tar.gz
/usr/localsudo tar -C /usr/local -xzf go1.22.4.linux-amd64.tar.gz
编辑 ~/.bashrc 或 ~/.bash_profile:
vim ~/.bashrc
添加以下内容:
export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
生效:
source ~/.bashrc
go version
输出类似:
go version go1.22.4 linux/amd64
CentOS 7:
sudo yum install golang -y
CentOS 8 / Stream:
sudo dnf install golang -y
查看版本:
go version
⚠ 缺点:版本通常较老(如 1.17)
go env -w GOPROXY=https://goproxy.cn,direct
go env -w GOSUMDB=off
查看配置:
go env
mkdir -p ~/go-projects/hello
cd ~/go-projects/hello
go mod init hello
hello.go
package main
import "fmt"
func main() {
fmt.Println("Hello, CentOS Go!")
}
运行:
go run hello.go
go: command not found/usr/local/go/bin 是否在 PATHsource ~/.bashrcGo 安装路径:/usr/local/go
GOPATH:~/go
GOPROXY:https://goproxy.cn
IDE:VS Code / GoLand
如果你用的是 CentOS 7 / 8 / Stream、ARM 服务器、或 Docker 环境,我可以给你更精确的配置方案。