英文:
Go + Revel: How to import custom package?
问题
我正在按照这里提供的聊天室教程进行操作。
我将其更改为导入本地包,而不是使用ravel的github上的示例。我在其中一个控制器(tuts中的refresh.go
)中将其更改为以下内容:
import (
"./../chatroom"
"github.com/revel/revel"
)
而chatroom位于正确的目录下:
- app
- chatroom
- chatroom.go
- controllers
- refresh.go
- app.go
chatroom
包在chatroom.go
中也已经初始化。
但是当运行代码时,我收到了以下错误:
Go代码app/tmp/main.go无法编译:非本地包中的本地导入"./../chatroom"
我在这里做错了什么?
英文:
I am following tutorial of chat room covered here
I changed it to import a local package instead of using the sample from ravel's github. I changed it into something like this in one of the controllers (refresh.go
in the tuts):
import (
"./../chatroom"
"github.com/revel/revel"
)
And chatroom was in the right directory:
- app
- chatroom
- chatroom.go
- controllers
- refresh.go
- app.go
package chatroom
was also initialized already in chatroom.go
.
But when running the code, I received this error:
The Go code app/tmp/main.go does not compile: local import "./../chatroom" in non-local package
What am I doing wrong here?
答案1
得分: 2
最好不要使用相对路径,而是使用从$GOPATH/src
开始的路径。
在你的情况下,如果$GOPATH/src
包含了app/chatroom
,你可以使用以下导入语句:
import app/chatroom
原帖评论中提到:
> 这样可以工作,但是我必须包含我的应用程序名称,类似于myapp/app/chatroom
,
如果$GOPATH/src
包含了myapp
文件夹,这样做是有道理的。
英文:
It would be best, following this answer to not use a relative path, but a path from the $GOPATH/src
In your case, is $GOPAHT/src
includes app chatroom, you would use
import app/chatroom
The OP comments:
> working, but I have to include my app name, something like myapp/app/chatroom
,
That makes sense, if $GOPATH/src
contains the folder myapp
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论