无法从GitHub私有仓库导入Go模块,请求技术支持
Go私有模块导入问题排查与解决
问题背景
我是Go新手,在GitHub个人账号下创建了Rainbow-Coloring-Books组织和api仓库,提交代码后尝试导入项目内其他包时,执行go mod tidy报错,已生成PAT并配置git,但问题未解决。
错误信息
go: finding module for package github.com/Rainbow-Coloring-Books/api/internal/service/user go: finding module for package github.com/Rainbow-Coloring-Books/api/internal/handler/user go: downloading github.com/Rainbow-Coloring-Books/api v0.0.0-20230324230419-1f509a56d7dc rainbowcoloringbooks/cmd/server imports github.com/Rainbow-Coloring-Books/api/internal/handler/user: github.com/Rainbow-Coloring-Books/api@v0.0.0-20230324230419-1f509a56d7dc: verifying module: github.com/Rainbow-Coloring-Books/api@v0.0.0-20230324230419-1f509a56d7dc: reading https://sum.golang.org/lookup/github.com/!rainbow-!coloring-!books/api@v0.0.0-20230324230419-1f509a56d7dc: 404 Not Found server response: not found: github.com/Rainbow-Coloring-Books/api@v0.0.0-20230324230419-1f509a56d7dc: invalid version: git ls-remote -q origin in /tmp/gopath/pkg/mod/cache/vcs/5b2a00aa68931b5d9107e6e4b2650ff812770ef5d19fc84a0904a0cb32002eed: exit status 128: fatal: could not read Username for 'https://github.com': terminal prompts disabled Confirm the import path was entered correctly. If this is a private repository, see https://golang.org/doc/faq#git_https for additional information. rainbowcoloringbooks/cmd/server imports github.com/Rainbow-Coloring-Books/api/internal/service/user: github.com/Rainbow-Coloring-Books/api@v0.0.0-20230324230419-1f509a56d7dc: verifying module: github.com/Rainbow-Coloring-Books/api@v0.0.0-20230324230419-1f509a56d7dc: reading https://sum.golang.org/lookup/github.com/!rainbow-!coloring-!books/api@v0.0.0-20230324230419-1f509a56d7dc: 404 Not Found server response: not found: github.com/Rainbow-Coloring-Books/api@v0.0.0-20230324230419-1f509a56d7dc: invalid version: git ls-remote -q origin in /tmp/gopath/pkg/mod/cache/vcs/5b2a00aa68931b5d9107e6e4b2650ff812770ef5d19fc84a0904a0cb32002eed: exit status 128: fatal: could not read Username for 'https://github.com': terminal prompts disabled Confirm the import path was entered correctly. If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.
项目目录结构
api/ ├── cmd/ │ └── server/ │ └── main.go ├── docker/ ├── docs/ ├── infrastructure/ ├── kubernetes/ ├── pkg/ └── internal/ ├── app/ ├── handler/ ├── repository/ └── service/
当前导入语句(main.go)
import ( userHandler "github.com/Rainbow-Coloring-Books/api/internal/handler/user" userService "github.com/Rainbow-Coloring-Books/api/internal/service/user" )
尝试过的无效导入方式:
import ( userService "github.com/Rainbow-Coloring-Books/api/blob/main/internal/service/user" userHandler "github.com/Rainbow-Coloring-Books/api/blob/main/internal/handler/user" )
当前go.mod内容
module rainbowcoloringbooks go 1.20 require github.com/gorilla/mux v1.8.0 require ( github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.12.0 // indirect github.com/leodido/go-urn v1.2.2 // indirect golang.org/x/crypto v0.7.0 // indirect golang.org/x/sys v0.6.0 // indirect golang.org/x/text v0.8.0 // indirect )
解决步骤
修正模块路径与导入路径匹配:你的
go.mod中module声明是rainbowcoloringbooks,但导入用了github.com/Rainbow-Coloring-Books/api/...,本地项目的模块路径必须和导入路径一致。修改go.mod第一行:module github.com/Rainbow-Coloring-Books/api go 1.20正确配置私有模块Git认证:Go拉取私有仓库需要认证,执行以下命令配置(替换
your-PAT为你的令牌):git config --global url."https://your-PAT@github.com/".insteadOf "https://github.com/"执行
git config --global --get-regexp url确认配置生效。禁用私有模块公共校验:私有模块校验和不会上传到公共服务器,执行命令跳过校验:
go env -w GOPRIVATE=github.com/Rainbow-Coloring-Books/*重新整理依赖:清理缓存并拉取依赖:
go clean -modcache go mod tidy本地开发导入注意:同一仓库内的包无需远程拉取,只要
go.mod路径正确,Go会直接引用本地文件,无需修改导入路径。
内容的提问来源于stack exchange,提问作者Saus
相关产品推荐
相关产品推荐

