Go App Engine项目的软件包结构 [英] Package structure for Go App Engine Project

查看:114
本文介绍了Go App Engine项目的软件包结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在遵循 Google App Engine的Go Tutorial



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

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

当我尝试引用路由器中的items.go .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};
}

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


  1. 我的项目目录应该位于Go SDK的 gopath 目录,或者它可以位于任何地方?

  2. li>


解决方案

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


  1. 是你的项目目录应该位于 GOPATH 。 Go期望按照描述工作区的文档中介绍的目录结构,其中包含位于 src 目录。编译时,它会在src文件夹下的gopath中查找软件包(如果已安装,则会pkg)。虽然App Engine文档没有具体说明这一点,但我的理解是,结构应该匹配 src 结构。


  2. 目录结构的一个例子是这样的:从设置 GOPATH = / myproject 开始。在 GOPATH 目录中,有以下结构(以你的一些软件包为例)。

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


当我构建时,我运行dev appserver给它MyApp文件夹 dev_appserver ./MyApp



我已经通过将 log .Println(< pkg>)在每个包 init() func中都是只运行一次,因为有些人表示如果你的结构错误,这是一个问题。


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?

解决方案

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.

这篇关于Go App Engine项目的软件包结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆