how to reference a package in another package having GO111MODULE=on in go

huangapple go评论86阅读模式
英文:

how to reference a package in another package having GO111MODULE=on in go

问题

如果我有以下结构:

HelperFolder
    Library1
        lib1.go
    Library2
        lib2.go
        file2.go

其中lib1.go导入Library2。

我在library2中执行了go mod init,然后执行了go mod tidy和go build,一切都正常。
但是我无法构建Library1。

我不想安装任何库,也不想将库放在gopath中(我已将HelperFolder放在不同的路径中),但我始终在构建library1时收到错误消息,指示package2不在GOROOT(C:\program files\go\src\package2)中。

我错过了什么?

感谢任何帮助。

英文:

If i have the below structure:

HelperFolder
    Library1
        lib1.go
    Library2
        lib2.go
        file2.go

Where lib1.go imports Library2.

I have executed go mod init in library2 and then go mod tidy and then go build and all is fine.
But I can't get Library1 built.

I do not want to install any of the libraries, or place the libraries in gopath (i have placed the HelperFolder in a different path) but i always get the error in building library1 that package2 is not in GOROOT(C:\program files\go\src\package2)

What am I missing?

any help is appreciated

答案1

得分: 2

一个 Go 模块可以包含多个包。定义多个模块的主要原因是可以在不同的发布计划上对这些模块中的包进行版本控制和发布。对于仅用于本地的(未发布、未版本化)包来说,通常更简单的做法是使用一个统一的模块。

也就是说,在单独的 Library 文件夹中运行 go mod init 两次,而是在 HelperFolder 中只运行一次。

英文:

A Go module may contain many packages. The main reason to define multiple modules is so that you can version and release the packages in those modules on separate release schedules. For local-only (unpublished, unversioned) packages, it is pretty much always simpler to instead use a single unified module.

That is: run go mod init just once in HelperFolder rather than twice in the individual Library folders.

答案2

得分: 0

根据@Zyl在他的评论中提到的,我使用了以下go命令:

go mod edit replace HelperFolder/Lib2 => D:\folderPath\HelperFolder\Lib2

然后执行:

go get HelperFolder/Lib2
英文:

As @Zyl noted in his comment, i used the following go commands:

go mod edit replace HelperFolder/Lib2 => D:\folderPath\HelperFolder\Lib2

Then

go get HelperFolder/Lib2

huangapple
  • 本文由 发表于 2021年8月27日 05:09:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/68945319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定