duanrong6802 2014-03-13 21:57
浏览 18
已采纳

Go App Engine专案的套件结构

I have been following Google App Engine's Go Tutorial.

According to the tutorial, I should create a root project directory and all source files related to the project should go into this directory. Consequentially, my workspace looks like this:

/MyProject
 /router
  router.go
 /items
  items.go

When I try to refer to items.go in router.go like this:

router.go

import(
    "items"
)

func itemsHandler(writer http.ResponseWriter, request * http.Request){
    anItem := items.Item{Id: 245,Name: "Chocolate",Price: 1.50};
}

The app fails to compile because items is undefined and now I am very confused about how a Go project on Google App Engine is supposed to be organised. What I'd like to know is

  1. Is my project directory supposed to be located in the Go SDK's gopath directory or can it be located anywhere? The Tutorial did not make this clear.

  2. What's the structure for a Go App Engine Project and how do I import the source files?

  • 写回答

1条回答 默认 最新

  • dongqing344716 2014-03-14 10:13
    关注

    It seems theh cause of your problem is a missing src directory. .

    1. Yes your project directory is supposed to be in the GOPATH. Go expects the directory structure to follow as described in the docs describing workspaces with all packages located in the src directory. When compiling it looks for packages in the gopath under the src folder (or pkg if installed). Although the App Engine docs do not specifically state this, it is my understanding that the structure should match the src structure.

    2. An example of the directory structure is like this: starting with setting GOPATH=/myproject. In the directory of GOPATH, have the following structure (using some of your packages as an example).

      /src
       /MyApp/app.yaml
       /MyApp/myappmain.go
       /items/items.go
       /router/router.go
      

    When I build I run the dev appserver by giving it the MyApp folder dev_appserver ./MyApp

    I have tested this by putting log.Println("<pkg>") in every package init() func and they all only get run once as there was some people indicating this was a problem if you got the structure wrong.

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?