登录
首页 >  Golang >  Go问答

无法导入 Golang 包到我的私人存储库

来源:stackoverflow

时间:2024-02-06 23:24:23 277浏览 收藏

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《无法导入 Golang 包到我的私人存储库》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

问题内容

对 golang 非常陌生,所以我确信我在这里遗漏了一些东西。非常感谢任何帮助。

我已经启动了一个新的 go 项目,并尝试从项目中的其他包导入模块。首先,我在 github 的个人帐户下创建了一个新组织,我们将其称为“rainbow-coloring-books”,向该组织添加了一个名为“api”的存储库,并添加并提交了一些基本的 go 代码。

我已经生成了个人访问令牌,并更新了我的 git 配置以在 pat 旁边包含我的 git 信息。

但是,当我尝试从存储库导入模块时,运行 go mod tidy 时出现以下错误:

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/[email protected]: verifying module: github.com/rainbow-coloring-books/[email protected]: reading https://sum.golang.org/lookup/github.com/!rainbow-!coloring-!books/[email protected]: 404 not found
        server response:
        not found: github.com/rainbow-coloring-books/[email protected]: 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/[email protected]: verifying module: github.com/rainbow-coloring-books/[email protected]: reading https://sum.golang.org/lookup/github.com/!rainbow-!coloring-!books/[email protected]: 404 not found
        server response:
        not found: github.com/rainbow-coloring-books/[email protected]: 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"
)

当尝试在浏览器中访问时,导入中使用的上述 url 会导致 github 上出现 404 页面,当我手动通过 ui 导航时,url 如下,但我尝试了这些导入,但不起作用,但我觉得不太合适:

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
)

我很好奇是否需要在组织/存储库级别执行某些操作来允许访问(可能需要使用 pat)? pat 只是附加到我的个人 github 上,但该组织是通过我的个人帐户创建的,我是成员,并且我拥有所需的所有权限/可以毫无问题地读取和写入存储库。

我已经在这方面投入了一段时间了,非常感谢任何帮助,提前致谢!


正确答案


您的 go.modmodule path”应该描述该模块的功能以及在哪里可以找到它。

所以很可能是 github.com/rainbow-coloring-books/api,使用 go mod edit 作为 illustrated here

go mod edit -module github.com/Rainbow-Coloring-Books/api

您的导入仍将以 api/ 开头,因为模块路径是模块内每个包的导入路径的前缀。

终于介绍完啦!小伙伴们,这篇关于《无法导入 Golang 包到我的私人存储库》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>