|
|
# Go 环境安装指南
|
|
|
|
|
|
**版本**: Go 1.21.6
|
|
|
**适用系统**: Windows / Linux / macOS
|
|
|
|
|
|
---
|
|
|
|
|
|
## 目录
|
|
|
|
|
|
1. [Windows 安装](#一windows-安装)
|
|
|
2. [Linux 安装](#二linux-安装)
|
|
|
3. [macOS 安装](#三macos-安装)
|
|
|
4. [验证安装](#四验证安装)
|
|
|
5. [配置 GOPROXY](#五配置-goproxy)
|
|
|
6. [常见问题](#六常见问题)
|
|
|
|
|
|
---
|
|
|
|
|
|
## 一、Windows 安装
|
|
|
|
|
|
### 方法一:使用 PowerShell 脚本(推荐)
|
|
|
|
|
|
1. **打开 PowerShell**(以管理员身份)
|
|
|
|
|
|
2. **运行安装脚本**
|
|
|
```powershell
|
|
|
cd d:\fs_workspace\market-data-service\scripts
|
|
|
.\install-go-windows.ps1
|
|
|
```
|
|
|
|
|
|
3. **等待安装完成**,脚本会自动:
|
|
|
- 下载 Go 1.21.6 安装包
|
|
|
- 执行安装
|
|
|
- 配置环境变量
|
|
|
- 设置国内镜像
|
|
|
|
|
|
4. **重新打开 PowerShell**,验证安装
|
|
|
```powershell
|
|
|
go version
|
|
|
```
|
|
|
|
|
|
### 方法二:手动安装
|
|
|
|
|
|
1. **下载安装包**
|
|
|
|
|
|
访问官方下载页面:
|
|
|
```
|
|
|
https://go.dev/dl/go1.21.6.windows-amd64.msi
|
|
|
```
|
|
|
|
|
|
2. **运行安装程序**
|
|
|
|
|
|
双击下载的 `.msi` 文件,按向导完成安装
|
|
|
|
|
|
3. **验证安装**
|
|
|
|
|
|
打开命令提示符,运行:
|
|
|
```cmd
|
|
|
go version
|
|
|
```
|
|
|
|
|
|
应输出:
|
|
|
```
|
|
|
go version go1.21.6 windows/amd64
|
|
|
```
|
|
|
|
|
|
### 环境变量配置
|
|
|
|
|
|
如果手动安装后 `go` 命令不可用,需要手动配置环境变量:
|
|
|
|
|
|
1. **右键"此电脑"** → **属性** → **高级系统设置**
|
|
|
|
|
|
2. **环境变量** → **系统变量** → **Path**
|
|
|
|
|
|
3. **添加以下路径**:
|
|
|
```
|
|
|
C:\Program Files\Go\bin
|
|
|
```
|
|
|
|
|
|
4. **新建用户变量**:
|
|
|
- 变量名:`GOPATH`
|
|
|
- 变量值:`%USERPROFILE%\go`
|
|
|
|
|
|
5. **重启命令提示符**
|
|
|
|
|
|
---
|
|
|
|
|
|
## 二、Linux 安装
|
|
|
|
|
|
### 方法一:使用安装脚本(推荐)
|
|
|
|
|
|
1. **打开终端**
|
|
|
|
|
|
2. **运行安装脚本**
|
|
|
```bash
|
|
|
cd /path/to/market-data-service/scripts
|
|
|
chmod +x install-go-linux.sh
|
|
|
./install-go-linux.sh
|
|
|
```
|
|
|
|
|
|
3. **使环境变量生效**
|
|
|
```bash
|
|
|
source ~/.bashrc
|
|
|
# 或
|
|
|
source ~/.zshrc
|
|
|
```
|
|
|
|
|
|
4. **验证安装**
|
|
|
```bash
|
|
|
go version
|
|
|
```
|
|
|
|
|
|
### 方法二:使用包管理器
|
|
|
|
|
|
**Ubuntu/Debian:**
|
|
|
```bash
|
|
|
# 添加 PPA
|
|
|
sudo add-apt-repository ppa:longsleep/golang-backports
|
|
|
sudo apt update
|
|
|
|
|
|
# 安装 Go
|
|
|
sudo apt install golang-1.21
|
|
|
|
|
|
# 创建软链接
|
|
|
sudo ln -s /usr/lib/go-1.21/bin/go /usr/local/bin/go
|
|
|
```
|
|
|
|
|
|
**CentOS/RHEL:**
|
|
|
```bash
|
|
|
# 使用 EPEL
|
|
|
sudo yum install epel-release
|
|
|
sudo yum install golang
|
|
|
|
|
|
# 或下载二进制
|
|
|
cd /tmp
|
|
|
wget https://go.dev/dl/go1.21.6.linux-amd64.tar.gz
|
|
|
sudo tar -C /usr/local -xzf go1.21.6.linux-amd64.tar.gz
|
|
|
```
|
|
|
|
|
|
### 方法三:手动安装
|
|
|
|
|
|
1. **下载安装包**
|
|
|
```bash
|
|
|
cd /tmp
|
|
|
wget https://go.dev/dl/go1.21.6.linux-amd64.tar.gz
|
|
|
```
|
|
|
|
|
|
2. **解压到 /usr/local**
|
|
|
```bash
|
|
|
sudo tar -C /usr/local -xzf go1.21.6.linux-amd64.tar.gz
|
|
|
```
|
|
|
|
|
|
3. **配置环境变量**
|
|
|
|
|
|
编辑 `~/.bashrc` 或 `~/.zshrc`:
|
|
|
```bash
|
|
|
export PATH=$PATH:/usr/local/go/bin
|
|
|
export GOPATH=$HOME/go
|
|
|
export PATH=$PATH:$GOPATH/bin
|
|
|
export GOPROXY=https://goproxy.cn,direct
|
|
|
```
|
|
|
|
|
|
4. **使配置生效**
|
|
|
```bash
|
|
|
source ~/.bashrc
|
|
|
```
|
|
|
|
|
|
5. **验证安装**
|
|
|
```bash
|
|
|
go version
|
|
|
```
|
|
|
|
|
|
---
|
|
|
|
|
|
## 三、macOS 安装
|
|
|
|
|
|
### 方法一:使用 Homebrew(推荐)
|
|
|
|
|
|
1. **安装 Homebrew**(如未安装)
|
|
|
```bash
|
|
|
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
|
|
|
```
|
|
|
|
|
|
2. **安装 Go**
|
|
|
```bash
|
|
|
brew install go@1.21
|
|
|
```
|
|
|
|
|
|
3. **添加到 PATH**
|
|
|
```bash
|
|
|
echo 'export PATH="/opt/homebrew/opt/go@1.21/bin:$PATH"' >> ~/.zshrc
|
|
|
source ~/.zshrc
|
|
|
```
|
|
|
|
|
|
4. **验证安装**
|
|
|
```bash
|
|
|
go version
|
|
|
```
|
|
|
|
|
|
### 方法二:使用安装脚本
|
|
|
|
|
|
1. **下载并运行脚本**
|
|
|
```bash
|
|
|
cd /path/to/market-data-service/scripts
|
|
|
chmod +x install-go-linux.sh
|
|
|
./install-go-linux.sh
|
|
|
```
|
|
|
|
|
|
2. **使环境变量生效**
|
|
|
```bash
|
|
|
source ~/.zshrc
|
|
|
```
|
|
|
|
|
|
### 方法三:手动安装
|
|
|
|
|
|
1. **下载安装包**
|
|
|
```bash
|
|
|
cd /tmp
|
|
|
curl -L -o go1.21.6.darwin-amd64.tar.gz https://go.dev/dl/go1.21.6.darwin-amd64.tar.gz
|
|
|
```
|
|
|
|
|
|
2. **解压**
|
|
|
```bash
|
|
|
sudo tar -C /usr/local -xzf go1.21.6.darwin-amd64.tar.gz
|
|
|
```
|
|
|
|
|
|
3. **配置环境变量**
|
|
|
|
|
|
编辑 `~/.zshrc`:
|
|
|
```bash
|
|
|
export PATH=$PATH:/usr/local/go/bin
|
|
|
export GOPATH=$HOME/go
|
|
|
export PATH=$PATH:$GOPATH/bin
|
|
|
export GOPROXY=https://goproxy.cn,direct
|
|
|
```
|
|
|
|
|
|
4. **使配置生效**
|
|
|
```bash
|
|
|
source ~/.zshrc
|
|
|
```
|
|
|
|
|
|
---
|
|
|
|
|
|
## 四、验证安装
|
|
|
|
|
|
### 4.1 基本验证
|
|
|
|
|
|
```bash
|
|
|
# 查看 Go 版本
|
|
|
go version
|
|
|
# 输出: go version go1.21.6 xxx/xxx
|
|
|
|
|
|
# 查看 Go 环境
|
|
|
go env
|
|
|
|
|
|
# 查看特定变量
|
|
|
go env GOPATH
|
|
|
go env GOROOT
|
|
|
go env GOPROXY
|
|
|
```
|
|
|
|
|
|
### 4.2 运行测试程序
|
|
|
|
|
|
创建测试文件 `hello.go`:
|
|
|
|
|
|
```go
|
|
|
package main
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
func main() {
|
|
|
fmt.Println("Hello, Go!")
|
|
|
fmt.Printf("Go version: %s\n", runtime.Version())
|
|
|
}
|
|
|
```
|
|
|
|
|
|
运行:
|
|
|
```bash
|
|
|
go run hello.go
|
|
|
```
|
|
|
|
|
|
应输出:
|
|
|
```
|
|
|
Hello, Go!
|
|
|
Go version: go1.21.6
|
|
|
```
|
|
|
|
|
|
### 4.3 编译测试
|
|
|
|
|
|
```bash
|
|
|
# 编译
|
|
|
go build -o hello hello.go
|
|
|
|
|
|
# 运行
|
|
|
./hello # Linux/macOS
|
|
|
hello.exe # Windows
|
|
|
```
|
|
|
|
|
|
---
|
|
|
|
|
|
## 五、配置 GOPROXY
|
|
|
|
|
|
### 为什么要配置?
|
|
|
|
|
|
Go 默认使用国外代理,在国内下载依赖可能很慢或失败。
|
|
|
|
|
|
### 配置方法
|
|
|
|
|
|
**临时配置(当前终端)**
|
|
|
```bash
|
|
|
# Windows
|
|
|
go env -w GOPROXY=https://goproxy.cn,direct
|
|
|
|
|
|
# Linux/macOS
|
|
|
export GOPROXY=https://goproxy.cn,direct
|
|
|
```
|
|
|
|
|
|
**永久配置**
|
|
|
|
|
|
Windows:
|
|
|
```powershell
|
|
|
[Environment]::SetEnvironmentVariable("GOPROXY", "https://goproxy.cn,direct", "User")
|
|
|
```
|
|
|
|
|
|
Linux/macOS:
|
|
|
```bash
|
|
|
echo 'export GOPROXY=https://goproxy.cn,direct' >> ~/.bashrc
|
|
|
source ~/.bashrc
|
|
|
```
|
|
|
|
|
|
### 验证配置
|
|
|
|
|
|
```bash
|
|
|
go env GOPROXY
|
|
|
# 输出: https://goproxy.cn,direct
|
|
|
```
|
|
|
|
|
|
### 其他可用代理
|
|
|
|
|
|
| 代理地址 | 说明 |
|
|
|
|----------|------|
|
|
|
| `https://goproxy.cn` | 七牛云,国内推荐 |
|
|
|
| `https://goproxy.io` | 官方,全球可用 |
|
|
|
| `https://mirrors.aliyun.com/goproxy/` | 阿里云 |
|
|
|
| `https://proxy.golang.org` | Google 官方 |
|
|
|
|
|
|
---
|
|
|
|
|
|
## 六、常见问题
|
|
|
|
|
|
### Q1: 安装后 `go` 命令不可用
|
|
|
|
|
|
**原因**: 环境变量未配置或需要重启终端
|
|
|
|
|
|
**解决**:
|
|
|
```bash
|
|
|
# Windows: 重新打开 PowerShell
|
|
|
# Linux/macOS:
|
|
|
source ~/.bashrc
|
|
|
# 或
|
|
|
source ~/.zshrc
|
|
|
```
|
|
|
|
|
|
### Q2: 下载依赖超时
|
|
|
|
|
|
**原因**: 未配置 GOPROXY 或网络问题
|
|
|
|
|
|
**解决**:
|
|
|
```bash
|
|
|
go env -w GOPROXY=https://goproxy.cn,direct
|
|
|
```
|
|
|
|
|
|
### Q3: 权限不足(Linux/macOS)
|
|
|
|
|
|
**解决**:
|
|
|
```bash
|
|
|
# 使用 sudo 运行安装脚本
|
|
|
sudo ./install-go-linux.sh
|
|
|
|
|
|
# 或手动解压到用户目录
|
|
|
tar -C $HOME/.local -xzf go1.21.6.linux-amd64.tar.gz
|
|
|
export PATH=$PATH:$HOME/.local/go/bin
|
|
|
```
|
|
|
|
|
|
### Q4: 如何卸载 Go
|
|
|
|
|
|
**Windows:**
|
|
|
1. 控制面板 → 程序和功能 → 卸载 Go
|
|
|
2. 删除环境变量中的 Go 相关配置
|
|
|
|
|
|
**Linux/macOS:**
|
|
|
```bash
|
|
|
# 删除安装目录
|
|
|
sudo rm -rf /usr/local/go
|
|
|
|
|
|
# 删除环境变量配置(编辑 ~/.bashrc 或 ~/.zshrc)
|
|
|
```
|
|
|
|
|
|
### Q5: 安装多个 Go 版本
|
|
|
|
|
|
使用 `g` 版本管理器:
|
|
|
```bash
|
|
|
# 安装 g
|
|
|
go install github.com/voidint/g@latest
|
|
|
|
|
|
# 安装特定版本
|
|
|
g install 1.21.6
|
|
|
g install 1.20.0
|
|
|
|
|
|
# 切换版本
|
|
|
g use 1.21.6
|
|
|
```
|
|
|
|
|
|
---
|
|
|
|
|
|
## 七、下一步
|
|
|
|
|
|
安装完成 Go 后,可以继续:
|
|
|
|
|
|
1. **返回项目目录**
|
|
|
```bash
|
|
|
cd d:\fs_workspace\market-data-service
|
|
|
```
|
|
|
|
|
|
2. **下载依赖**
|
|
|
```bash
|
|
|
go mod download
|
|
|
```
|
|
|
|
|
|
3. **启动服务**
|
|
|
```bash
|
|
|
go run ./cmd/server
|
|
|
```
|
|
|
|
|
|
4. **访问管理后台**
|
|
|
```
|
|
|
http://localhost:8080/admin
|
|
|
```
|
|
|
|
|
|
---
|
|
|
|
|
|
## 参考资源
|
|
|
|
|
|
- [Go 官方下载](https://go.dev/dl/)
|
|
|
- [Go 官方安装文档](https://go.dev/doc/install)
|
|
|
- [Go 国内镜像](https://goproxy.cn/)
|
|
|
|
|
|
---
|
|
|
|
|
|
**文档结束**
|