英文:
Good way to use and test local packages in Google App Engine apps written in golang?
问题
我有一个问题,我正在测试使用本地包的GAE golang应用程序。
项目的结构大致如下:
.
├── app.yaml
├── main.go
└── handler
└── handler.go
handler
包在 main.go
中被导入。
package main
import "handler"
一切都很好(例如,goapp serve
),直到我开始编写测试。
goapp test
抱怨找不到 handler
包。似乎 goapp serve
和 goapp test
的 GOPATH
是不同的。我找到的一个解决方案是将 handler
包放在项目路径之外,并使用完全限定的名称导入它(例如,github.com/.../handler
),但这对我来说没有意义,因为它将项目拆分成紧密耦合的不同位置。有没有好的方法来使用和测试本地包?
在这个主题上找到了以下资源。
- https://groups.google.com/forum/#!topic/google-appengine-go/C_i5kQEi7-A
- https://github.com/matthewbelisle-wf/gopath-problem
英文:
I have a problem of testing my GAE golang app uses local packages.
The probject looks something like this.
.
├── app.yaml
├── main.go
└── handler
└── handler.go
handler
package is imported in main.go
.
package main
import "handler"
Everything (e.g., goapp serve
) works fine until I started writing tests.
goapp test
complains that handler
package is not found. Seems like GOPATH
of goapp serve
and goapp test
are different. One solution I found is to put
handler
package outside of the project path and import it with fully
qualified name (e.g., github.com/.../handler
) but it doesn't make sense to me
to split the project in to separate places where they're tightly coupled. Are
there any good way to use and test local packages?
Following resources are found on this topic.
答案1
得分: 2
这个有点晚了,但如果有人遇到同样的问题,以下是处理方法:
-
按照以下方式组织你的代码:
/whereever └── /myproject └──/src ├── app.yaml ├── main.go └── /handler └── handler.go
-
在项目代码中使用不带限定符的导入,例如:
import handler
-
从 src 文件夹内运行
goapp serve
和goapp deploy
。serve/deploy 相对于 app.yaml 工作。cd /whereever/myproject/src goapp serve
-
为
goapp test
设置 GOPATH,它基于 GOPATH 运行。cd /whereever/myproject #GOPATH 需要一个名为 src 的子文件夹 GOPATH=$GOPATH:`pwd` #将当前文件夹添加到 GOPATH goapp test ./... #运行所有子文件夹中的测试
-
如有必要,重置你的 GOPATH。
source ~/.profile #假设你已经在 ~/.profile 中永久设置了 GOPATH
总的来说,"按预期工作",但仍然有些麻烦。
英文:
This comes late, but if anyone runs into the same issue, here is how to deal with it:
-
Organize your code this way
/whereever └── /myproject └──/src ├── app.yaml ├── main.go └── /handler └── handler.go
-
Use unqualified imports for your project code e.g.
import handler
-
Run
goapp serve
andgoapp deploy
from inside the src folder. serve/deploy work relative to the app.yamlcd /whereever/myproject/src goapp serve
-
Set GOPATH for
goapp test
, it work based on GOPATHcd /whereever/myproject #GOPATH expects a subfolder called src GOPATH=$GOPATH:`pwd` #add the current folder to the GOPATH goapp test ./... #run all tests in any subfolders
-
Reset your GOPATH if necessary
source ~/.profile #this assumes ~/.profile is where you have permanently set your GOPATH
All in all, "working as intended", but still a pain.
答案2
得分: 1
你需要使用完全限定名称导入handler,但不需要将其移出项目。如果你的项目文件夹结构如下:
/go
└── /myproject
├── app.yaml
├── main.go
└── /handler
└── handler.go
那么你的handler导入应该是这样的:
import "myproject/handler"
英文:
You need to import handler with a fully qualified name, however you don't need to move it out of the project to do so. If your project folder looks like this:
/go
└── /myproject
├── app.yaml
├── main.go
└── /handler
└── handler.go
Then your handler import should look like this:
import "myproject/handler"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论