Golang构建命令如何查找私有GitLab仓库

Golang构建命令如何查找私有GitLab仓库 我在使用 go.mod 和私有 GitLab 仓库时遇到了困难。当我尝试构建项目时,go 试图下载我的仓库并收到 410 Gone 错误。

系统:

windows 10

我的项目结构:

私有仓库: gitlam.com/me/private

结构:

pkg1
pkg2
cmd
    |_prj
       |_main.go
go.mod
go.sum

我在 private/cmd/prj1 目录下工作。

go.mod:

module gitlab.com/me/private

go 1.13.4

我尝试过的:

每次都是同样的 410 错误……我已经没有主意了。也许有人有解决方案?或者 go 不支持在私有仓库的 cmd 文件夹中工作?


更多关于Golang构建命令如何查找私有GitLab仓库的实战教程也可以访问 https://www.itying.com/category-94-b0.html

4 回复

很高兴看到现在一切如预期般正常运行。 请将该问题标记为已解决。点击灰色的勾选框。

更多关于Golang构建命令如何查找私有GitLab仓库的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


我想我找到了解决方案 😊 在 cmd/prj 目录下添加 go.mod 文件,并加入这行代码:

replace gitlab.com/me/private => ../../

注意! /path/to/repository 必须包含 go.mod 文件。查看文档

不清楚您想要实现什么。

如果您希望包不从 proxy.golang.org 下载,那么 set GOPRIVATE=gitlab.com/me/private 应该就足够了。这就是 GOPRIVATE 环境变量的目的。请参阅 go help module-private

您是否正在从这个私有仓库导入包?

为了测试您的问题,我创建了一个新项目,其 go.mod 模块名为 gitlab.com/me/private。 在项目中,我创建了一个名为 pkg1 的包,并在 cmd/app 目录中创建了一个 main.go 文件。main 函数调用了该包中的一个函数。导入路径是 gitlab.com/me/private/pkg1

我使用命令 GOPRIVATE="gitlab.com/me/private" go build ./cmd/app 来构建它。

它静默地编译成功,存储在 go.mod 文件旁边的名为 app 的可执行文件按预期运行。如您所见,go build 不需要访问 gitlab 仓库来构建项目。所以我不明白问题出在哪里,以及为什么您需要这个 replace 指令。我不需要它。

在私有GitLab仓库中使用Go模块时遇到410错误,通常是因为Go工具链无法正确认证访问私有仓库。以下是针对Windows 10系统的解决方案:

1. 配置Git凭证(最关键的步骤)

在Windows上,使用Git凭证管理器存储认证信息:

# 清除旧的Git配置
git config --global --unset url.https://gitlab.com/me/private.insteadof

# 设置Git使用凭证管理器
git config --global credential.helper manager

# 添加GitLab认证配置
git config --global url."https://gitlab.com".insteadOf "https://gitlab.com"

然后运行以下命令让Git缓存凭证:

git ls-remote https://gitlab.com/me/private.git

这会提示输入用户名和密码/访问令牌。

2. 配置Go环境变量

在PowerShell或CMD中设置:

# 设置私有仓库
$env:GOPRIVATE = "gitlab.com/me/private"

# 确保代理和校验被绕过
$env:GONOPROXY = "gitlab.com/me/private"
$env:GONOSUMDB = "gitlab.com/me/private"

# 可选:设置Git重写规则
git config --global url."git@gitlab.com:".insteadOf "https://gitlab.com/"

3. 使用SSH替代HTTPS(推荐)

修改go.mod文件使用SSH协议:

// go.mod
module gitlab.com/me/private

go 1.13.4

// 如果需要引用其他私有模块,使用SSH格式
require (
    gitlab.com/me/other-private v0.0.1
)

// 使用replace指令强制使用SSH
replace gitlab.com/me/private => git@gitlab.com:me/private.git

配置SSH密钥并添加到GitLab:

# 生成SSH密钥
ssh-keygen -t ed25519 -C "your-email@example.com"

# 将公钥添加到GitLab账户
# ~/.ssh/id_ed25519.pub 内容复制到GitLab SSH Keys设置

4. 使用.netrc文件(Windows)

C:\Users\你的用户名\_netrc文件中添加:

machine gitlab.com
login your-username
password your-personal-access-token

确保文件权限正确:

# 设置文件为仅当前用户可读
icacls C:\Users\你的用户名\_netrc /inheritance:r
icacls C:\Users\你的用户名\_netrc /grant:r "%USERNAME%:R"

5. 完整的环境配置脚本

创建setup-go-private.ps1脚本:

# PowerShell脚本
$env:GOPRIVATE = "gitlab.com/me/private"
$env:GONOPROXY = "gitlab.com/me/private"
$env:GONOSUMDB = "gitlab.com/me/private"

# 配置Git
git config --global credential.helper manager
git config --global url."https://gitlab.com".insteadOf "https://gitlab.com"

# 清理Go模块缓存
go clean -modcache

# 验证配置
go env GOPRIVATE GONOPROXY GONOSUMDB

6. 如果仍然遇到410错误

尝试直接使用go get命令测试:

# 清理并重新获取依赖
go clean -modcache
rm go.sum
go mod tidy

# 或者强制重新下载
go get -u -v gitlab.com/me/private@latest

7. 检查GitLab配置

确保:

  • 个人访问令牌有read_repository权限
  • 仓库设置为私有但可访问
  • 访问令牌未过期
  • 项目路径大小写正确

8. 工作目录问题

关于在cmd/prj目录下工作的问题,Go模块支持在任何子目录中工作。确保在项目根目录执行:

# 在项目根目录(go.mod所在目录)执行
cd /path/to/private
go build ./cmd/prj

如果需要在子目录中构建,使用相对路径:

# 在cmd/prj目录中
go build .

410 Gone错误通常表示GitLab端的问题,但通过上述认证配置可以解决Go工具链的访问问题。

回到顶部