如何使用“GOPATH"之外的模块在另一个模块中? [英] How to use a module that is outside of "GOPATH" in another module?
问题描述
我已经使用这个命令go mod init database"在database"文件夹中的GOPATH"之外创建了一个库作为个人使用的模块,但我不知道:
I've created a library as the module for personal use outside of "GOPATH" in "database" folder with this command "go mod init database," and I don't know:
- 如何在另一个模块中使用/导入这个模块?
操作系统:Windows 7,转到:v1.11
推荐答案
最简单且开箱即用的解决方案是将您的 database
包/模块放入 VCS(例如 github.com),所以其他包(在其他模块内)可以简单地通过导入来引用它:
The easiest and working out-of-the-box solution is to put your database
package / module into a VCS (e.g. github.com), so other packages (inside other modules) can simply refer to it by importing it like:
import "github.com/someone/database"
如果你这样做,你甚至不必手动摆弄 go.mod
文件,一切都会由 go 工具处理:它会自动识别并解决这个依赖, 下载并安装所需的包,也会自动更新go.mod
.
If you do so, you don't even have to fiddle with the go.mod
files manually, everything will be taken care of by the go tool: it will automatically recognize and resolve this dependency, download and install the required package, and will also update go.mod
automatically.
如果您不想使用 VCS(例如,您只是在尝试或尚未决定使用什么),那么您仍然可以使用.how 在官方 Go Wiki:我可以在本地文件系统上完全脱离 VCS 工作吗?
If you don't want to use a VCS (e.g. you're just experimenting or you haven't decided what to use yet), then you can still do it. The how is detailed in the official Go Wiki: Can I work entirely outside of VCS on my local filesystem?
因此,您在 GOPATH
之外创建了一个 database
文件夹,并在其中创建了一个模块.你创建了另一个模块,我们称之为 main
,你想使用这个 database
包.
So you created a database
folder outside of GOPATH
, and you created a module in it. And you created another module, let's call it main
, and you want to use this database
package.
你必须做的是:
go.mod
您的main
模块必须将database
包列为要求".为您的database
包提供一个临时的 VCS 名称:
go.mod
of yourmain
module must list thedatabase
package as a "requirement". Give a temporary VCS name to yourdatabase
package:
require (
example.com/me/database v0.0.0
)
你必须告诉go工具这个包所在的位置,因为我们使用的完整包名只是一个临时/幻想的名字.使用 replace
指令使这个 database
包指向本地磁盘上的文件夹;您可以使用绝对路径和相对路径:
You must tell the go tool where this package is located, because the full package name we used is just a temporary / fantasy name. Use the replace
directive to make this database
package point to a folder on your local disk; you may use absolute and relative paths:
replace example.com/me/database => ../database
仅此而已.
让我们看一个工作示例.让我们创建一个 pretty
模块.创建一个 pretty
文件夹,里面有 2 个文件:
Let's see a working example. Let's create a pretty
module. Create a pretty
folder with 2 files in it:
pretty.go:
package pretty
import "fmt"
func Pretty(v ...interface{}) {
fmt.Println(v...)
}
go.mod(可以通过运行go mod init pretty
来创建):
go.mod (can be created by running go mod init pretty
):
module pretty
现在让我们创建另一个主模块.让我们在 pretty
文件夹旁边创建一个文件夹 osinf
(它可能是任何东西).其中有 2 个文件:
Now let's create another, main module. Let's create a folder osinf
(it may be whatever) next to the pretty
folder. 2 files in it:
osinf.go(注意我们打算使用我们的 pretty
包/模块,我们通过 "example.com/me/pretty" 导入它
代码>):
osinf.go (note we intend to use our pretty
package / module, we import it by "example.com/me/pretty"
):
package main
import "example.com/me/pretty"
func main() {
pretty.Pretty("hi")
pretty.Pretty([]int{1, 3, 5})
}
go.mod:
module main
require example.com/me/pretty v0.0.0
replace example.com/me/pretty => ../pretty
仅此而已.
在osinf
文件夹中运行go run osinf.go
,输出为:
Running go run osinf.go
in the osinf
folder, the output is:
hi
[1 3 5]
这篇关于如何使用“GOPATH"之外的模块在另一个模块中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!