英文:
REST application folder structure in golang
问题
我刚刚开始学习Go语言,作为第一个项目,我决定制作一个简单的REST API服务器。不幸的是,我遇到了一个问题,我似乎找不到最佳的项目结构方式。
到目前为止,我正在使用与Rails项目相同的结构,即将控制器和模型放在各自的文件夹中,但这并不符合Go代码的惯例。
有人能给我关于如何正确组织项目结构的建议吗?
英文:
I'm just getting started with Go and as a first project I decided to make a simple REST API server. Unfortunately, I've hit a problem as I can't seem to find the best way to structure my project.
So far, I'm using the same structure as a Rails project meaning having controllers and models in their own folders, but it doesn't as idiomatic as go code should be.
Does anyone have any advice on how to properly structure a project?
答案1
得分: 2
这可能是可行的,但由于静态编译的原因,如果你想要使用Rails的自动路由功能,可能会有一些混乱。
我建议创建一个models
包来存放所有的数据库模型,然后使用Gorilla的mux来将一些路由绑定到一些操作上。
如果情况变得复杂,你可以尝试使用像Revel这样的更大型的框架,它可能更适合用于大型的MVC应用程序。
英文:
It's probably do-able, but because of static compilation you're going to have a bit of a mess if you want a lot of Rails' automagic for figuring out routes.
I'd probably just have a models
package with all the database models, and then something based on Gorilla's mux to bind some routes to some actions.
If things are getting complicated you can try a larger framework like Revel, which might be more suitable for a larger MVC application.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论