5 回答

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"
(您不導入模塊。您從模塊導入包。)

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

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"

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

TA貢獻1854條經驗 獲得超8個贊
去 mod init?api.com
// "api.com" 命名您的應用程序,它類似于 swift 的包標識符,您也可以 "whatever.youlike"
- 5 回答
- 0 關注
- 303 瀏覽
添加回答
舉報