Its been long since working with go language, been awesome experience so far. This post is regarding how one can structure go application in MVC structure like Rails. This example would be using Martini and Gorm, for demonstration purpose only.
So, to start we would have application directory structure contaning
1.) config: config folder will contain all config files as needed and/or migrations
2.) models: models will contain all models referencing to their counterpart in database tables
3.) controllers: as usual this will contain all controllers
4.) templates: all templates will go to this directory, though this cane be named as views.
5.) server.go: this file will contain main function and routing, though routing can also be moved to seperate file.
Now what we would be developing, is a activity model named models/activity.go packaged as models, here is snippet of same
but wait, we could move fields common for all models into seperate generic model, here is how it looks after
and our beloved activity.go
Now Activity can be referred as models.Activity wherever needed (but of-course we need to import it)
Next we would first look into our server.go file contaning main function for our app, Here is how it looks
So what we are doing here is to import relevent packages as needed. Map gorm.DB to martini context and declare routing. Do not worry about what controllers.IndexActivities would be doing. We would come to that next.
Next, we need to define out activities controller as controllers/activities under controllers package.
Here is how our activities controller looks like
Just to wrap it up, we could define our migrations under config/migrations.go, here is how would that looks like