[英]Can't import local project from other project: cannot find module providing package <package_name>
I have a project I am working on named project1
.我有一个名为project1
的项目。
Couple of months ago I worked on project2
which contains package named engine
and I want to use it inside project1
.几个月前,我在 project2 工作,其中包含名为engine
的project2
,我想在project1
中使用它。
These projects are local so I read here how to import local projects and tried it but received an error:这些项目是本地的,所以我在这里阅读了如何导入本地项目并尝试过但收到错误:
Cannot resolve file `project2`
One interesting thing is that when I type the name of project2
in the import(..)
section, Goland identify it as module but after I press on it I received the error that it can't be resolve.一件有趣的事情是,当我在import(..)
部分中键入 project2 的名称时, project2
将其识别为模块,但在我按下它之后,我收到了无法解决的错误。
With Goland I have an option to run sync packages of 'project1'
but when I pressed on it I also received an error:使用 Goland,我可以选择运行sync packages of 'project1'
但是当我按下它时,我也收到了一个错误:
project1/pkg/utils imports
project2: cannot find module providing package project2
I also tried to create vendor
folder in project1
and copy-paste the whole project2
beneath the vendor
folder but it still didn't help.我还尝试在project1
中创建vendor
文件夹并将整个project2
复制粘贴到vendor
文件夹下,但它仍然没有帮助。
Any idea why it doesn't being resolve?知道为什么它没有得到解决吗?
If you have both of your projects under your $GOPATH
, you can check out this example for importing projects.如果您的两个项目都在$GOPATH
下,则可以查看此示例以导入项目。
EDIT: If you are using go modules
and want to import local modules, then you can make use of the replace
directive.编辑:如果您使用go modules
并想要导入本地模块,那么您可以使用replace
指令。 So, basically you have to add in your go.mod
of your Project1
these lines:所以,基本上你必须在你的Project1
的go.mod
中添加这些行:
require /$module-name-project2/$package-name v0.0.0
replace $module-name-project2/$package-name => ../$localpath-to-project2
A quick example (both my projects are outside of $GOPATH
and using go modules
):一个简单的例子(我的两个项目都在$GOPATH
之外并使用go modules
):
Project1
is located under .../go-experiments/project1
Project1
位于.../go-experiments/project1
下main.go
: main.go
:
package main
import "go-experiments/project2/greeting"
func main() {
println("How to greet?")
greeting.English()
}
go.mod
: go.mod
:
module go-experiments/project1
require go-experiments/project2/greeting v0.0.0
replace go-experiments/project2/greeting => ../project2
go 1.14
Project2
is located under .../go-experiments/project2
greeter.go
: Project2
位于.../go-experiments/project2
greeter.go
:package greeting
func English() {
println("hi, i am boo")
}
go.mod
: go.mod
:
module go-experiments/project2
go 1.14
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.