REST application folder structure in golang

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

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.

huangapple
  • 本文由 发表于 2015年5月1日 20:47:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/29987122.html
匿名

发表评论

匿名网友

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

确定