# go: 无法锁定模块缓存：/home/user/go/pkg/mod/cache/download/lock：权限被拒绝

- **ID:** `go/module-cache-lock-error`
- **领域:** go
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

用户对模块缓存锁定文件没有写权限，通常是由于以不同用户身份运行 Go 或在受限环境中运行。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.11 | active | — | — |
| 1.12 | active | — | — |
| 1.13 | active | — | — |
| 1.14 | active | — | — |
| 1.15 | active | — | — |
| 1.16 | active | — | — |
| 1.17 | active | — | — |
| 1.18 | active | — | — |
| 1.19 | active | — | — |
| 1.20 | active | — | — |
| 1.21 | active | — | — |
| 1.22 | active | — | — |
| 1.23 | active | — | — |

## 解决方案

1. **Change ownership of the module cache to the current user** (90% 成功率)
   ```
   sudo chown -R $(whoami) $(go env GOMODCACHE)
   ```
2. **Set GOMODCACHE to a writable directory** (85% 成功率)
   ```
   export GOMODCACHE=/tmp/gomodcache && go mod download
   ```

## 无效尝试

- **Running 'sudo go build' to bypass permissions** — Running Go as root can cause ownership issues and security risks; may also not resolve if the cache is owned by another user. (60% 失败率)
- **Deleting the lock file manually** — The lock file is managed by Go; deletion may cause race conditions or corruption. (80% 失败率)
