【问题标题】:How do I resolve "cannot find module for path X" importing a local Go module?如何解决导入本地 Go 模块的“找不到路径 X 的模块”?
【发布时间】:2022-02-12 23:29:40
【问题描述】:

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

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

cannot find module for path X

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

module X

构建或安装此模块似乎无济于事。我在$GOPATH/pgk/mod 中没有发现它的迹象。

我尝试了各种导入语句:

救命!

【问题讨论】:

标签: go vgo


【解决方案1】:

所以你编写了一个 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 专为公开发布的模块而设计。通常,一个模块的名称既是它的唯一标识符,也是它的公共仓库的路径。当您的 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,例如在将模块推送到公共仓库之前测试对模块的更改。但您也可以使用它将模块标识符绑定到您不打算发布的本地代码。

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

希望这会有所帮助。

【讨论】:

  • 天啊,希望这是谷歌上的第一名。在我最终得到这个正确的解释之前,我必须阅读很多东西。
  • 我花了很长时间才得出这个答案。非常感谢。
【解决方案2】:

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

例子:

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

【讨论】:

  • 我测试了这个GO111MODULE=off 选项并且它有效。
  • 如果我想拥有本地包 pakk 但也有其他外部模块,有没有办法做到这一点?现在,如果我将 GO111MODULE=off 关闭,似乎找不到外部包,例如“github.com/go-chi/chi”
【解决方案3】:

花了 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"

【讨论】:

  • 这比其他解决方案好很多
【解决方案4】:

去 mod init api.com

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

myExample

【讨论】:

    【解决方案5】:

    错误? 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 2015-06-21
      • 2021-08-18
      • 2020-04-13
      • 2021-10-16
      • 2018-08-24
      • 2020-07-06
      相关资源
      最近更新 更多