如何解决 "cannot find module for path X" 导入本地 Go 模块?

How do I resolve "cannot find module for path X" importing a local Go module?

在我的 Go 项目中,我想将一些通用功能分解为 Go 模块,与主项目分开。为了与 go 的未来保持一致,我在 GOPATH 之外做这件事。我不想在 GitHub 或其他任何地方发布该模块。

我将此模块导入主项目的所有尝试导致:

cannot find module for path X

我在模块的文件夹中 运行 go mod init X。它的 go.mod 文件的内容是:

module X

构建或安装此模块似乎没有任何作用。我在 $GOPATH/pgk/mod.

中没有发现它的迹象

我尝试了多种导入语句:

求助!

所以你写了一个 Go “库”模块 X 其中:

  • 您不想在 GitHub 或其他地方发布
  • 您想在您的项目(“主”模块)中导入和使用。

使用 replace 指令和 require

在主模块的 go.mod 中,添加以下行:

require "X" v0.0.0
replace "X" v0.0.0 => "{local path to the X module}"

路径应指向X的根目录。可以是绝对的也可以是相对的。

从模块 X 导入包 util:

import "X/util"

(您不导入模块。您从模块导入包。)


说明

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 使用 require 指令声明模块依赖,Go 将自动在该路径查找并检索指定版本的模块。

例如,如果您的 go.mod 文件包含 require github.com/some/dependency v1.2.3,Go 将从该路径的 GitHub 检索模块。但如果它包含 require X v0.0.0,“X”不是实际路径,您将收到错误 cannot find module for path X.

replace 指令允许您为给定的模块标识符和版本指定替换路径。有 many reasons you'd want to do this,例如在将它们推送到 public 存储库之前测试对模块的更改。但您也可以使用它将模块标识符绑定到您不打算发布的本地代码。

Go 模块文档中的更多详细信息:

希望这对您有所帮助。

如果您不想使用 Go 模块,则不需要。从 Go v1.13 开始,默认使用 go modules。因此,如果您不想这样做,您需要明确告知。

示例:

main.go:

package main

import (
    "fmt"

    "./pakk"
)

func main() {
    fmt.Println("Hello" + pakk.World())
}

pakk/pakk.go:

package pakk

func World() string {
    return " world"
}

在这种情况下,运行 go run main.go会输出

build command-line-arguments: cannot find module for path .../pakk

但是运行

GO111MODULE=off go run main.go

会输出

Hello world

花了 4 天时间弄明白了。很失望,但是导入的方式./X

如下:

首先发出这个魔法命令:

go mod init poop/strawberry/monkey

然后您可以轻松导入您的 ./X 文件夹:

import  (
"poop/strawberry/monkey/X"
)

文件夹结构:

./X/X.go
./main.go
./go.mod 

go.mod的内容:

module poop/strawberry/monkey

这是来自 go 模块创建者的绝妙解决方案

https://go.dev/ref/mod#go-mod-init

module path: A path that identifies a module and acts as a prefix for package import paths within the module. For example, "golang.org/x/net"

go mod init api.com

// "api.com" 为你的应用命名,它类似于 swift 的包标识符,你也可以 "whatever.youlike"

myExample

Error ? Go 无法解析模块的路径,这可能是由于错误配置或(未配置)用于项目依赖项的“go.mod”文件跟踪。

解决方案 假设您的项目文件夹如下所示;

/ |-- 文件夹一/ |-- 文件夹二/ |-- 文件夹三/ |-- main.go

 And the main.go script imports the modules  folderOne,folderTwo and folderFour's script (folderfour.go) imports the module folderThree.
    
    folderOne:
    execute in the commandline:
go mod init github.com/../folderOne (i.e path from github.com folder to folderOnes)
    The go mod init command creates a go.mod file to track your code's dependencies
    
    folderTwo:
    execute in the commandline:
go mod init github.com/../folderTwo (i.e path from github.com folder to folderTwos)
    The go mod init command creates a go.mod file to track your code's dependencies
    
    folderThree:
    execute in the commandline:
go mod init github.com/../folderThree (i.e path from github.com folder to folderThrees)
    The go mod init command creates a go.mod file to track your code's dependencies
    
    folderFour:
    execute in the commandline: 
go mod init github.com/../folderThree (i.e path from github.com folder to folderFour)
    Go to the folderFours script and import the module folderThree i.e 
    import "github.com/../folderThree"
    
    in folderfours commandline: 
$ go mod edit -replace github.com/{pathto}/folderThree=./folderThree**
    
    then execute: go mod tidy
    
    in your projects root folder execute the command: go mod init github.com/../ProjectRootDirectory (i.e path from github.com folder to ProjectRootDirectory)
    
    then to import the modules, i.e folderThree, folderTwo, folderOne
    execute the following at the projects root folder(ie folder with main.go)
$ go mod edit -replace github.com/{pathto}/folderOne=./folderOne
$ go mod edit -replace github.com/{pathto}/folderTwo=./folderTwo
$ go mod edit -replace github.com/{pathto}/folderFour=./folderFour
    
then execute;
$ go mod tidy
and then
$ go run main.go