为了账号安全,请及时绑定邮箱和手机立即绑定

如何解决导入本地 Go 模块时“找不到路径 X 的模块”?

如何解决导入本地 Go 模块时“找不到路径 X 的模块”?

Go
慕哥6287543 2023-03-29 15:32:38
在我的 Go 项目中,我想将一些通用功能分解为一个 Go 模块,与主项目分开。为了与 go 的未来保持一致,我在 GOPATH 之外做这件事。我不想在 GitHub 或其他任何地方发布该模块。我将此模块导入主项目的所有尝试都会导致:cannot find module for path Xgo mod init X我在模块的文件夹中运行。其文件内容go.mod为:module X构建或安装此模块似乎没有任何作用。我在里面没有发现它的迹象$GOPATH/pgk/mod。我尝试了多种导入语句:import "X"import "../x" (模块目录的相对路径)import "../x/X"(目录路径+模块名)帮助!
查看完整描述

5 回答

?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

所以你写了一个 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"

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

查看完整回答
反对 回复 2023-03-29
?
慕标琳琳

TA贡献1830条经验 获得超9个赞

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


例子:


主要去:


package main


import (

    "fmt"


    "./pakk"

)


func main() {

    fmt.Println("Hello" + pakk.World())

}

包/pack.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


查看完整回答
反对 回复 2023-03-29
?
喵喔喔

TA贡献1735条经验 获得超5个赞

花了 4 天时间弄明白了。非常失望,但是 import ./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"



查看完整回答
反对 回复 2023-03-29
?
MM们

TA贡献1886条经验 获得超2个赞

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


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


/ |-- folderOne/ |-- folderTwo/ |-- folderThree/ |-- 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


查看完整回答
反对 回复 2023-03-29
?
哔哔one

TA贡献1854条经验 获得超8个赞

去 mod init api.com

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

查看完整回答
反对 回复 2023-03-29
  • 5 回答
  • 0 关注
  • 151 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信