Go App Engine专案的套件结构

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?

我一直在关注 Google App Engine的Go教程。 p>

根据教程,我应该创建一个根项目目录,并且与该项目相关的所有源文件都应进入该目录。 因此,我的工作区如下所示: p>

  / MyProject 
 / router 
 router.go 
 / items 
 items.go 
  code>   pre> 
 
 

当我尝试在router.go中引用item.go时: p>

router.go strong> p>

  import(
“项目” 
)
 
func itemsHandler(编写器http.ResponseWriter,请求* http.Request){
 anItem:= items.Item {Id:245  ,名称:“ Chocolate”,价格:1.50}; 
} 
  code>  pre> 
 
 

由于未定义 items code>,现在应用无法编译 我对应该如何组织Google App Engine上的Go项目感到非常困惑。 我想知道的是 p>

  1. 我的项目目录应该位于Go SDK的 gopath code>目录中还是可以 它位于任何地方? 教程没有明确说明。 p> li>

  2. Go App Engine项目的结构是什么,如何导入源文件? p> li> ol> div>

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.