Go + Revel: How to import custom package?

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

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.

huangapple
  • 本文由 发表于 2014年11月2日 20:57:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/26699526.html
匿名

发表评论

匿名网友

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

确定