如何使用go get或go dep拉取GitLab子组私有依赖?报错求解
Let’s walk through the most common fixes for this error—usually, it’s either a path misconfiguration, permission issue, or Go mod not handling your private GitLab repo correctly.
1. Verify Your Dependency Path in go.mod
First, double-check your go.mod file to make sure you’re referencing the full correct path of your subcategory project. The error points to git.mydomain.com/myteam/category.git, which suggests Go is trying to pull a parent repo that doesn’t exist as a standalone module.
Ensure your require line looks like this:
require git.mydomain.com/myteam/category/subcategory/project v1.2.3 // replace with your actual version
Avoid typos or truncated paths here—even a missing subcategory can trigger this unwanted parent-repo lookup.
2. Confirm Git Access to the Private Repo
Go mod relies on Git to pull dependencies, so if you can’t clone the repo directly with Git, Go won’t be able to either.
Test SSH access: Run this command to verify your SSH key works with GitLab:
git clone git@git.mydomain.com:myteam/category/subcategory/project.gitIf this fails, double-check that your SSH public key is added to your GitLab account and that your private key is loaded in your SSH agent.
Switch to SSH for HTTPS paths (if needed): If you’ve been using HTTPS, add this to your
~/.gitconfigfile to force Go to use SSH instead (avoids credential prompts and permission issues):[url "git@git.mydomain.com:"] insteadOf = https://git.mydomain.com/
3. Configure GOPRIVATE for Your GitLab Domain
Go defaults to fetching public modules from proxy servers, but it needs explicit instructions to treat your GitLab domain as private. Set the GOPRIVATE environment variable:
- For Linux/macOS (add to
~/.bashrcor~/.zshrcto make it permanent):export GOPRIVATE=git.mydomain.com - For Windows Command Prompt:
set GOPRIVATE=git.mydomain.com - For Windows PowerShell:
$env:GOPRIVATE = "git.mydomain.com"
This tells Go not to use public proxies for your private repos and to fetch them directly via Git.
4. Check the Subproject's Module Declaration
If the subcategory project you’re depending on has an incorrect module line in its own go.mod, Go might try to resolve a parent module instead.
Open the go.mod file of git.mydomain.com/myteam/category/subcategory/project and make sure the first line is:
module git.mydomain.com/myteam/category/subcategory/project
A misconfigured module name here (like omitting subcategory/project) will cause Go to look for the parent category repo, which doesn’t exist.
5. Clean Go Mod Cache and Retry
Stale cache data can sometimes cause false "repo not found" errors. Clear the mod cache and re-fetch dependencies:
go clean -modcache go mod tidy
Bonus: CI/CD Environment Checks
If you’re running this in a CI pipeline (like GitLab CI), make sure:
- The CI runner has an SSH key with read access to the private repo, or you’re using a GitLab access token with
read_repositorypermissions. - The
GOPRIVATEvariable is set in your CI environment variables.
内容的提问来源于stack exchange,提问作者srimaln91

