英文:
Can't import a Go local module when module name starts with github.com, otherwise works fine
问题
我尝试在本地使用以下命令来设置我的Go模块:
go mod init mymodule/api
当我尝试在本地导入文件,比如import "mymodule/api/routes"
,它可以正常工作。然而,我尝试通过删除初始的go.mod
文件并运行以下命令来更改我的Go模块名称:
go mod init github.com/myrepo/project/api
然后,当我在其中一个模块中通过import "github.com/myrepo/project/api/routes"
导入文件时,它无法正常工作。它不会尝试在本地导入模块,而是尝试从github.com
获取模块。
确切的错误消息如下所示:
github.com/myrepo/project/api/routes: cannot find module providing package github.com/myrepo/project/api/routes: module github.com/myrepo/project/api/routes: git ls-remote -q origin in C:\Users\R\go\pkg\mod\cache\vcsc9cab82d2eef4d5003b0ef7010fbbba91353c1368fe1a69d6c6a8fbfa8c8eb5: exit status 128:
remote: Repository not found.
fatal: repository 'github.com/myrepo/project/api' not found
有人可以告诉我如何将我的Go模块重命名为以github.com
前缀开头,并且仍然可以在本地加载模块吗?或者这是预期的行为吗?
英文:
I tried using the following command for my Go module locally.
go mod init mymodule/api
When I try to import files locally, like import "mymodule/api/routes"
, it works. However, I tried changing my Go module's name by deleting the initial go.mod
file and running this command.
go mod init github.com/myrepo/project/api
Then, when I import the file in one of my modules by doing import "github.com/myrepo/project/api/routes"
, it doesn't work. It doesn't try to import the module locally, instead it tries to fetch a module from github.com
.
The exact error message is the following.
github.com/myrepo/project/api/routes: cannot find module providing package github.com/myrepo/project/api/routes: module github.com/myrepo/project/api/routes: git ls-remote -q origin in C:\Users\R\go\pkg\mod\cache\vcsc9cab82d2eef4d5003b0ef7010fbbba91353c1368fe1a69d6c6a8fbfa8c8eb5: exit status 128:
remote: Repository not found.
fatal: repository 'github.com/myrepo/project/api' not found
Can anyone let me know how I can rename my Go module to start with a github.com prefix and still load the modules locally? Or is this the expected behavior?
答案1
得分: 2
开发状态:
当你想要同时开发或调试这两个包时,可以使用Golang文档中的replace
指令。replace
指令用于将特定版本或所有版本的模块内容替换为其他位置的内容。替换可以使用另一个模块路径和版本,或者使用特定于平台的文件路径来指定。
因此,你可以使用replace
命令来实现这个目的,将准确的包地址替换为你本地包的地址:
replace (
github.com/myrepo/project/api => ../myLocalRepo/Project/Api
)
要了解有关replace
的更多信息,请阅读此文档:https://go.dev/ref/mod#go-mod-file-replace
生产状态:
但是,如果你想在生产环境中使用它,这取决于你的包。如果你的包在GitHub上是私有的,最具体的选项是将GOPRIVATE
设置为与你的模块路径完全匹配。像这样:
GOPRIVATE=github.com/your_github_username/project/apit
英文:
DEVELOPMENT STATE:
When you wanted to develop both or debug both of the packages.
from the Golang documentation:
> replace directive replaces the contents of a specific version of a module, or all versions of a module, with contents found elsewhere. The replacement may be specified with either another module path and version, or a platform-specific file path.
so you can use replace command for this purpose, by replacing the exact package address with your local package address:
replace (
github.com/myrepo/project/api => ../myLocalRepo/Project/Api
)
for more information about replace
, read this documentation: https://go.dev/ref/mod#go-mod-file-replace
PRODUCTION STATE:
but if you wanted to use it in production. it depends on your package, if your package is private on GitHub: the most specific option would be setting GOPRIVATE
to match the path of your module exactly. like this:
GOPRIVATE=github.com/your_github_username/project/apit
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论