如何解决“找不到路径 X 的模块"?导入本地 Go 模块? [英] How do I resolve "cannot find module for path X" importing a local Go module?
问题描述
在我的 Go 项目中,我想将一些通用功能分解为一个 Go 模块,与主项目分开.为了与 Go 的未来保持一致,我在 GOPATH 之外做这件事.我不想在 GitHub 或其他任何地方发布该模块.
In my Go project, I want to break out some generic functionality into a Go module, separate from the main project. I'm doing this outside of GOPATH in keeping with go's future. I don't want to publish the module on GitHub or anywhere else.
我将这个模块导入主项目的所有尝试都导致:
All my attempts to import this module into the main project result in:
cannot find module for path X
我已经在模块的文件夹中运行了 go mod init X
.它的go.mod
文件内容为:
I've run go mod init X
in the module's folder. The contents of its go.mod
file is:
module X
构建或安装此模块似乎没有任何作用.我在 $GOPATH/pgk/mod
中没有发现它的迹象.
Building or installing this module seems to do nothing. I've found no sign of it in $GOPATH/pgk/mod
.
我尝试了多种导入语句:
I've tried a variety of import statements:
导入X"
import "../x"
(相对路径到模块目录)import "../x/X"
(目录路径+模块名)
import "X"
import "../x"
(relative path to the module directory)import "../x/X"
(path to the directory + module name)
帮助!
推荐答案
所以你写了一个 Go库"模块 X
其中:
So you wrote a Go "library" module X
which:
- 您不想在 GitHub 或其他地方发布
- 您想导入并在您的项目中使用(main"模块).
在主模块的 go.mod
中,添加以下几行:
In your main module's go.mod
, add the following lines:
require "X" v0.0.0
replace "X" v0.0.0 => "{local path to the X module}"
路径应该指向X
的根目录.它可以是绝对的,也可以是相对的.
The path should point to the root directory of X
. It can be absolute or relative.
从模块X导入包util:
import "X/util"
(您不导入模块.您从模块导入包.)
(You don't import modules. You import packages from modules.)
Go 的模块功能专为公开发布的模块而设计.通常,模块的名称既是其唯一标识符,也是其公共存储库的路径.当您的 go.mod 使用 require<声明模块依赖项时/code> 指令,Go 将自动在该路径查找并检索指定版本的模块.
Go's module functionality is designed for publicly published modules. Normally, a module's name is both its unique identifier and the path to its public repo. When your go.mod declares a module dependency with the require
directive, Go will automatically find and retrieve the specified version of the module at that path.
例如,如果您的 go.mod
文件包含 require github.com/some/dependency v1.2.3
,Go 将在该路径从 GitHub 检索模块.但是如果它包含 require X v0.0.0
,X"不是实际路径,您将收到错误 cannot find module for path X
.
If, for example, your go.mod
file contains require github.com/some/dependency v1.2.3
, Go will retrieve the module from GitHub at that path. But if it contains require X v0.0.0
, "X" isn't an actual path and you will get the error cannot find module for path X
.
replace
指令允许您为给定的模块标识符和版本指定替换路径.您想要的原因有很多这样做,例如在将更改推送到公共存储库之前测试对模块的更改.但您也可以使用它来将模块标识符绑定到您不打算发布的本地代码.
The replace
directive allows you to specify a replacement path for a given module identifier and version. There are many reasons you'd want to do this, such as to test changes to a module before pushing them to the public repo. But you can also use it to bind a module identifier to local code you don't ever intend to publish.
Go Modules 文档中的更多详细信息:
More details in the Go Modules documentation:
希望这会有所帮助.
这篇关于如何解决“找不到路径 X 的模块"?导入本地 Go 模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!