CentOS 7.6 安装 Go 1.20.12 环境教程+更换国内源

发布时间 2023-12-09 14:57:55作者: 深圳第一深情

安装

因为需要安装httpx,官方github要求使用1.20版本的Go环境,就没有安装最新的1.21。

先去官网查看:https://go.dev/dl/
image

  1. 如上图,我们选择Linux amd64的(使用命令下就行,如若不能正常下载,就直接下完传上服务器也一样)

wget https://go.dev/dl/go1.20.12.linux-amd64.tar.gz
2. 其次我们需要将它解压至 /usr/local

tar -C /usr/local -zxvf go1.20.12.linux-amd64.tar.gz

  1. 设置环境变量

sudo vi /etc/profile

  1. 在最后一行加上

export PATH=$PATH:/usr/local/go/bin

  1. 然后加载配置文件

source /etc/profile

image

换源

参考:https://learnku.com/go/wikis/38122

众所周知,国内网络访问国外资源经常会出现不稳定的情况。 Go 生态系统中有着许多中国 Gopher 们无法获取的模块,比如最著名的 golang.org/x/...,并且在中国大陆从 GitHub 获取模块的速度也有点慢。

因此设置 CDN 加速代理就很有必要了,以下是几个速度不错的提供者:

    七牛:Goproxy 中国 https://goproxy.cn
    阿里: mirrors.aliyun.com/goproxy/
    官方: < 全球 CDN 加速 https://goproxy.io/>

在 Linux 或 macOS 上面,需要运行下面命令(或者,可以把以下命令写到 .bashrc 或 .bash_profile 文件中):

  1. 启用 Go Modules 功能
    go env -w GO111MODULE=on

  2. 配置 GOPROXY 环境变量,以下三选一

2.1. 七牛 CDN
go env -w GOPROXY=https://goproxy.cn,direct

2.2. 阿里云
go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct

2.3. 官方
go env -w GOPROXY=https://goproxy.io,direct

确认一下:

$ go env | grep GOPROXY
GOPROXY="https://goproxy.cn"

image