亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何解決導入本地 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 關注
  • 303 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號