Go 中的应用程序开发
我正在用 Go 编写一个 Web 应用程序。到目前为止,我已经编写了我现在需要的所有基础库,并且效果非常完美。包结构在这方面确实很方便。
现在我需要开始编写应用程序本身,即业务逻辑。我有点不知道这段代码应该如何或去哪里。
我已经观看并阅读了官方文档页面上的几乎所有应用程序开发教程/演练,他们总是在 main
包/命名空间中编写整个域逻辑。
这是要走的路(抱歉,不得不这样做)还是只是为了简单起见?
在我看来,将业务层放入包中感觉是错误的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,它们不必全部位于同一个包中。您可以轻松创建一个新目录,将该包的代码放在那里,然后从
main
包中导入它。就这么简单。您可能希望将
go-tour
视为一个简单的网络应用程序,其中包含一些小包(pic
、wc
、...)。如果您想了解大型 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.