如何解决“找不到路径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:
-
import "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库"模块,该模块:
So you wrote a Go "library" module which:
- 您不想在GitHub或其他地方发布
- 您要导入它并在项目(主"模块)中使用它.
使用
replace
指令解决此问题.
Use the
replace
directive to solve this problem.
给出模块名称"X",在主模块中添加以下行:
Given the module name "X" as you've called it, in your main module add the following lines:
require "X" v0.0.0
replace "X" v0.0.0 => "{local path to the X module}"
路径应指向模块的根目录,并且可以是绝对路径或相对路径.
The path should point to the root directory of the module, and can be absolute or relative.
要从模块 X 中导入软件包 util :
To import package util from module X:
import "X/util"
(您不导入模块.您从模块导入软件包.)
(You don't import modules. You import packages from modules.)
Go的模块功能是为公开发布的模块设计的.通常,模块的名称既是其唯一标识符,又是其公共存储库的路径.当您的 go.mod 用require
指令声明模块依赖性时,请转到会在该路径下自动查找和检索模块的指定版本.
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.
(See also in the Go Modules FAQ, Can I work entirely outside of VCS on my local filesystem?)
例如,如果您的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.
希望这会有所帮助.
这篇关于如何解决“找不到路径X模块"?导入本地Go模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!