在Go语言中的应用程序开发

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

Application development in Go

问题

我正在用Go编写一个Web应用程序。到目前为止,我已经编写了所有我现在需要的基础库,并且效果非常好。在这方面,包的结构非常方便。

现在我到了需要开始编写应用程序本身,也就是业务逻辑的地方。我有点迷茫,不知道这段代码应该放在哪里。

我已经观看和阅读了官方文档页面上几乎所有的应用程序开发教程/演示,它们总是将整个领域逻辑写在main包/命名空间中。

这是Go的方式吗(抱歉,不得不这样说),还是只是为了简单起见?

在我看来,将业务层放入包中感觉不太对劲。

英文:

I'm writing a web application in Go. So far I've written all base libraries I'm going to need for now and that worked out just perfect. The package structure is really handy in that aspect.

Now I'm at the point where I need to start writing the application itself, the business logic. I'm a little lost of how or where this code should go.

I've watched and read pretty much every application development tutorial/walkthrough on the official documentation page and they always write the entire domain logic in the main package/namespace.

Is this the way to Go (sorry, had to) or was it just for the sake of simplicity?

Putting the business layer into packages feels uncomfortably wrong in my opinion.

答案1

得分: 5

不,它不必全部放在同一个包中。你可以轻松地创建一个新的目录,将该包的代码放在那里,并从你的main包中导入它。就是这么简单。

你可能想看一下go-tour,它是一个由几个小包(picwc,...)组成的简单的Web应用程序。如果你想看一个大型Go应用程序的组织方式,可以看看Camlistore。这两个都是由Go的作者编写的,所以可以认为它们是很好的示例。

我只想说,在Go中编写小型、可重用和独立的包非常常见,这减少了应用程序的主要代码库的大小。

英文:

No, it does not have to be all in the same package. You can easily make a new directory, put your code for that package there, and import it from your main package. It's simple as that.

You may want to look at go-tour as a simple web app that consists of a few small packages (pic, wc, ...). If you want to see organization of a big Go app see Camlistore. Both of these are written by Go Authors, so they can be considered nice examples.

I just have to say that writing small, reusable, and separate packages is very common in Go, and that reduces the size of the main codebase of apps.

huangapple
  • 本文由 发表于 2012年1月26日 02:38:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/9008282.html
匿名

发表评论

匿名网友

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

确定