英文:
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
,它是一个由几个小包(pic
,wc
,...)组成的简单的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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论