duange2971 2016-04-14 07:06
浏览 45
已采纳

GO项目的Appengine文件夹结构

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:

Folder structure

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:

https://groups.google.com/forum/#!searchin/google-appengine-go/init/google-appengine-go/dNhqV6PBqVc/Pm2HBrhdCAAJ

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条回答 默认 最新

  • dreamy1992 2016-04-14 12:35
    关注

    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/"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能