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 关注
- 206 浏览
添加回答
举报