英文:
Appengine folder structure for GO project
问题
我的应用程序在使用Appengine的开发软件时运行良好。但是当我尝试部署时,出现以下错误:
main.go:11: 找不到导入: "github.com/afoo/cohort/models"
我的文件夹结构如下,其中afoo是一个"standard" src/github.com设置的子目录,被引用在GOPATH中:
app.yaml文件非常简单:
application: application-id
version: 1
runtime: go
api_version: go1
handlers:
- url: /styles
static_dir: styles
- url: /scripts
static_dir: scripts
- url: /.*
script: _go_app
main.go文件中有我定义的URL处理程序函数,例如:
func init() {
// Register a handler for /.
http.HandleFunc("/", MapNetworkHandler)
}
main.go中的模块导入是完全限定的,看起来像这样:
"github.com/afoo/cohort/models"
Google groups在2014年有一个长篇讨论,试图解释正确的文件夹设置。它位于这里:
从那个讨论中,我了解到我需要将所有模块(这里是models、repository和utils)与主文件夹分开。但是,然后有一些令人困惑的提到其他应该放在每个模块文件夹中的.yaml文件,没有描述它们的外观,还有对一个骨架init (.go)文件的暗示,该文件以某种方式导入其他所有文件。
无论好坏,我通过示例来学习,但我找不到一个能帮助我的好例子。如果有人能提供关于正确的文件夹结构和辅助文件的详细说明,我将非常感激。
谢谢任何帮助!
英文:
My app works fine using Appengine's development software. When I try to deploy, I get this error:
main.go:11: can't find import: "github.com/afoo/cohort/models"
My folder structure looks like this, where afoo is a subdirectory of a "standard" src/github.com setup, referenced in the GOPATH:
The app.yaml file is quite simple:
application: application-id
version: 1
runtime: go
api_version: go1
handlers:
- url: /styles
static_dir: styles
- url: /scripts
static_dir: scripts
- url: /.*
script: _go_app
The main.go file has the URL handler funcs I've defined, for instance:
func init() {
// Register a handler for /.
http.HandleFunc("/", MapNetworkHandler)
}
Module imports in main.go are fully qualified, and look like this:
"github.com/afoo/cohort/models"
Google groups has a long discussion from 2014 that attempts to explain the proper folder setup. It's located here:
From that discussion, I learned that I need to separate all modules (here, models, repository, and utils) from the main folder. But then there are some confusing mentions of other .yaml files that should go into each module folder, with no description of how they look, along with allusions to a skeleton init (.go) file that somehow imports everything else.
For better or worse, I learn by example, and I can't find a good example that would help me. I'd sure appreciate any detailed explanation of the proper folder structures and supplementary files that someone might know about.
Thanks for any help!
答案1
得分: 0
对于位于项目文件夹中的代码,请使用相对于 app.yaml
的路径。
所以如果你有:
- 文件夹:MyProject(可以在GOPATH/github或其他任何地方)
- app.yaml
- 文件夹:MyCode(包名为mycode)
- code1.go
- 文件夹:MySubCode(包名为mysubcode)
- code2.go
导入应该如下所示:
import (
"mycode"
"mycode/mysubcode"
)
请注意,在 app.yaml
下方或与其处于同一级别的代码中,不需要指定github或其他任何内容。
更新:我将项目文件夹放在GOPATH之外。例如:
- ~(用户文件夹)
- GOPATH文件夹
- 我的GAE项目文件夹
MyProject1
这样可以同时从类似于“github/”的GOPATH导入和从相对于app.yaml的路径导入,例如“myproject/”。
英文:
For code that is in your project folder use path relative to app.yaml
.
so if you have:
- Folder: MyProject (can be inside GOPATH/github or any other place)
- app.yaml
- Folder: MyCode (package mycode)
- code1.go
- Folder: MySubCode (package mysubcode)
- code2.go
The imports should be like:
import (
"mycode"
"mycode/mysubcode"
)
Note that you do not specify the github or anything else above the app.yaml
for code that is is below or on the same level as app.yaml.
Update: I keep project folder outside of GOPATH. Like:
- ~ (user folder)
- GOPATH folder
- My GAE projects folder
MyProject1
This way it works fine with both importing from GOPATH's like "github/" and from paths relative to app.yaml like "myproject/"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论