Package structure for Go App Engine Project

huangapple go评论94阅读模式
英文:

Package structure for Go App Engine Project

问题

我一直在按照Google App Engine的Go教程进行操作。

根据教程,我应该创建一个根项目目录,所有与项目相关的源文件都应该放在这个目录中。因此,我的工作空间看起来像这样:

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

当我尝试在router.go中引用items.go时,像这样:

router.go

import(
	"items"
)

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

应用程序无法编译,因为items未定义,现在我对Google App Engine上的Go项目应该如何组织感到非常困惑。我想知道的是:

  1. 我的项目目录应该位于Go SDK的gopath目录中,还是可以位于任何位置?教程没有明确说明这一点。

  2. 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

得分: 7

看起来你的问题的原因是缺少了src目录。

  1. 是的,你的项目目录应该在GOPATH中。Go语言期望目录结构遵循文档中描述的工作区,所有的包都应该位于src目录下。在编译时,它会在GOPATHsrc目录(或者pkg目录,如果已安装)下查找包。尽管App Engine文档没有明确说明这一点,但我理解的是目录结构应该与src结构相匹配。

  2. 目录结构的示例如下:首先设置GOPATH=/myproject。在GOPATH的目录下,有以下结构(以你的一些包为例)。

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

当我构建时,通过将MyApp文件夹传递给开发服务器来运行开发服务器dev_appserver ./MyApp

我通过在每个包的init()函数中放置log.Println("<pkg>")进行了测试,它们只会运行一次,因为有些人指出如果目录结构错误,这可能是一个问题。

英文:

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(&quot;&lt;pkg&gt;&quot;) 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.

huangapple
  • 本文由 发表于 2014年3月14日 05:57:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/22391847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定