Revel和golang的导入是否太过物理?

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

Revel and golang import too physical?

问题

刚开始使用Go/Revel。我将"examples/booking"应用程序复制到"examples/booking2"目录中,这样我就可以在不影响原始应用程序的情况下进行修改。

当我运行它时,得到了奇怪的结果,特别是数据库中的初始数据似乎被复制了。

我追踪到了问题的根源:

import "github.com/revel/examples/booking/app/models"

一旦我将所有相关的导入中的"booking"改为"booking2",一切都正常了。但我首先想到的是"为什么导入路径是这么物理的?"。我在想,在应用程序内部是否有一种相对引用应用程序各个包的方式,而不必引用应用程序所在的物理路径。

有没有办法做到这一点?

我甚至不确定这是"Go"的事情,还是"Revel"的事情。

谢谢。

英文:

just starting with go/revel. I copied the "examples/booking" app into "examples/booking2" directory so i could muck with it w/o impacting the original.

When I ran it I got weird results, in particular the initial data in the db seemed to be duplicated.

I tracked this down to the imports:

import "github.com/revel/examples/booking/app/models"

Once I changed "booking" to "booking2" in all the relevant imports things were fine. But my first thought is "why is an import path so physical?". I'm thinking that within the application there should be a way to relatively reference the app's various packages w/o having to reference the physical path where the app resides.

Is there a way to do that?

Not even sure if this is "go" thing, or a "revel" thing.

Thx.

答案1

得分: 1

导入路径直接映射到文件系统路径,相对于$GOPATH/src/

https://golang.org/doc/code.html#ImportPaths

看起来你可以这样进行相对导入:

../other-pkg

但这不是惯用的或推荐的方法(https://stackoverflow.com/questions/10687627/relative-import-from-parent-directory)。

英文:

The import path maps directly to the filesystem path, relative to $GOPATH/src/.

https://golang.org/doc/code.html#ImportPaths

It seems that you can do a relative import like this:

../other-pkg

But it's not idiomatic or recommended.

huangapple
  • 本文由 发表于 2017年8月23日 07:39:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/45828485.html
匿名

发表评论

匿名网友

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

确定