Files
go-common/SETUP.md
2025-11-30 13:15:13 +08:00

175 lines
3.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 项目配置说明
## GOPRIVATE 环境变量配置
由于本项目使用私有 Git 仓库 (`git.toowon.com`),需要配置 `GOPRIVATE` 环境变量,让 Go 工具链知道这些模块是私有的,不要通过公共代理下载。
### 配置方法
#### 方法一:临时配置(当前终端会话有效)
```bash
# macOS/Linux
export GOPRIVATE=git.toowon.com
# Windows (PowerShell)
$env:GOPRIVATE="git.toowon.com"
# Windows (CMD)
set GOPRIVATE=git.toowon.com
```
#### 方法二:永久配置(推荐)
**macOS/Linux:**
1. 编辑 shell 配置文件(根据你使用的 shell 选择):
```bash
# 如果是 zshmacOS 默认)
nano ~/.zshrc
# 如果是 bash
nano ~/.bashrc
# 或
nano ~/.bash_profile
```
2. 添加以下内容:
```bash
export GOPRIVATE=git.toowon.com
```
3. 保存文件并重新加载配置:
```bash
# zsh
source ~/.zshrc
# bash
source ~/.bashrc
```
**Windows:**
1. 打开"系统属性" -> "高级" -> "环境变量"
2. 在"用户变量"或"系统变量"中点击"新建"
3. 变量名:`GOPRIVATE`
4. 变量值:`git.toowon.com`
5. 点击"确定"保存
#### 方法三:使用 go env 命令推荐Go 1.13+
```bash
# 设置 GOPRIVATE
go env -w GOPRIVATE=git.toowon.com
# 查看当前配置
go env GOPRIVATE
# 如果需要设置多个私有仓库,用逗号分隔
go env -w GOPRIVATE=git.toowon.com,github.com/your-org
```
### 验证配置
```bash
# 查看 GOPRIVATE 配置
go env GOPRIVATE
# 应该输出: git.toowon.com
```
### 其他相关环境变量(可选)
如果需要更细粒度的控制,还可以配置:
```bash
# GOPRIVATE: 私有模块,不通过代理下载,不校验 checksum
go env -w GOPRIVATE=git.toowon.com
# GONOPROXY: 不通过代理下载的模块(默认与 GOPRIVATE 相同)
go env -w GONOPROXY=git.toowon.com
# GONOSUMDB: 不校验 checksum 的模块(默认与 GOPRIVATE 相同)
go env -w GONOSUMDB=git.toowon.com
```
### Git 认证配置
由于是私有仓库,还需要配置 Git 认证:
#### 方法一:使用 SSH推荐
1. 确保已配置 SSH 密钥
2. 使用 SSH URL
```bash
git config --global url."git@git.toowon.com:".insteadOf "https://git.toowon.com/"
```
#### 方法二:使用 HTTPS + 个人访问令牌
1. 在 Git 服务器上生成个人访问令牌
2. 配置 Git 凭据:
```bash
git config --global credential.helper store
```
3. 首次访问时会提示输入用户名和令牌
#### 方法三:在 URL 中包含凭据(不推荐,安全性较低)
```bash
# 在 go.mod 中使用(不推荐)
# 或者通过 .netrc 文件配置
```
### 常见问题
#### 问题1go get 失败,提示找不到模块
**解决方案:**
1. 确认 GOPRIVATE 已正确配置
2. 确认 Git 认证已配置
3. 尝试手动克隆仓库验证:
```bash
git clone git@git.toowon.com:jimmy/go-commom.git
```
#### 问题2go mod download 失败
**解决方案:**
```bash
# 清除模块缓存
go clean -modcache
# 重新下载
go mod download
```
#### 问题3IDE 无法识别模块
**解决方案:**
1. 重启 IDE
2. 在 IDE 中执行:`go mod tidy`
3. 确认 IDE 的 Go 环境变量配置正确
### 完整配置示例
```bash
# 1. 配置 GOPRIVATE
go env -w GOPRIVATE=git.toowon.com
# 2. 配置 Git SSH如果使用 SSH
git config --global url."git@git.toowon.com:".insteadOf "https://git.toowon.com/"
# 3. 验证配置
go env | grep GOPRIVATE
# 4. 测试模块下载
go get git.toowon.com/jimmy/go-commom@latest
```
### 参考文档
- [Go Modules 官方文档](https://go.dev/ref/mod)
- [Go 私有模块配置](https://go.dev/ref/mod#private-modules)