如何使用"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:
- 如何在另一个模块中使用/导入该模块?
- How to use/import this module in another module?
OS: 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(例如,您只是在尝试或尚未决定使用什么),那么您仍然可以使用它. 方式在官方
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
必须将database
软件包列为要求".为您的database
软件包指定一个临时的VCS名称:
main
模块的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屋!