Subfolders and packages when using Appengine and Go

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

Subfolders and packages when using Appengine and Go

问题

在部署和/或测试一个使用appengine的Go项目时,可以使用appcfg.py和dev_appserver.py工具来编译项目。

当所有的*.go文件都在一个文件夹中时,这个方法可以正常工作。但是,如果代码被分成了子文件夹,同时还需要访问彼此的函数和常量,该如何编译项目呢?

在Go中,子文件夹在定义上是包的边界,我无法找到一种方法来让appengine工具在测试或部署之前编译多个包,从一个项目中。

除了将所有内容都放在一个文件夹中之外,对于如何解决这个问题的建议将不胜感激。即使解决方案是逐个处理每个包(文件夹),也希望能提供关于如何为使用所有这些包的一个项目快速迭代的结构的建议。谢谢!

英文:

When deploying and/or testing a Go project for appengine, the appcfg.py and dev_appserver.py tools are used to compile the project

That works fine when all the *.go files are in one folder, but how can a project be be compiled when code is divided into subfolders- yet also need to access functions and constants from eachother?

In Go- subfolders are by definition package boundaries, and I can't see a way to allow the appengine tools to compile multiple packages before testing or deploying, from one project.

Advice on how to tackle this- other than keeping it all in one folder, is appreciated. Even if the solution is to approach it one package (folder) at a time, advice on how to structure that for quick iteration on one project that uses all those packages is appreciated. Thanks!

答案1

得分: 1

这里没有什么魔法。只需确保每个Go文件都放在一个文件夹中,而不是放在您的appengine项目的根目录中。

例如:

-[项目根目录]
    app.yaml
    -[packagea]
        packagea.go
        -[packageab]
            packageab.go
    -[packageb]
            packageb.go

在上面的示例中,包声明应如下所示:

  • packagea.go:

      package packagea
    
  • packageab.go

      package packageab
    
  • packageb.go

      package packageb
    

例如,如果packageb同时使用了packageapackageab,则导入它们的方式如下:

  • packageb.go:

      import (
          "packagea"
          "packagea/packageab"
      )
    

注意:

请注意,您不能创建循环导入(例如,packagea导入packageb,而packageb导入packagea):

规范:导入声明

> 导入声明声明了导入包和被导入包之间的依赖关系。一个包不能直接或间接地导入自身,这是非法的。

您必须结构化您的代码和包以避免导入循环,否则您的代码将无法编译。

英文:

There is no magic here. Just make sure each of your go files is placed in a folder and not in the root of your appengine project.

For example:

-[Project root]
    app.yaml
    -[packagea]
        packagea.go
        -[packageab]
            packageab.go
    -[packageb]
            packageb.go

In the example above, package declarations should be as follows:

  • packagea.go:

      package packagea
    
  • packageab.go

      package packageab
    
  • packageb.go

      package packageb
    

And for example if packageb uses both packagea and packageab, import them as:

  • packageb.go:

      import (
          "packagea"
          "packagea/packageab"
      )
    

Note:

Note that you cannot create an import cycle (e.g. packagea imports packageb, and packageb imports packagea):

Spec: Import declarations

> An import declaration declares a dependency relation between the importing and imported package. It is illegal for a package to import itself, directly or indirectly.

You have to structure your code and packages to avoid import cycles or your code will not compile.

huangapple
  • 本文由 发表于 2015年9月21日 15:55:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/32690040.html
匿名

发表评论

匿名网友

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

确定