title: How to access Gorm in Revel Controller?
body: <p>let me start by saying these are my first couple days of toying around in Go.</p>
  
  <p>I'm trying to use the Revel framework with Gorm like this:</p>
  
  <p><code>app/controllers/gorm.go</code></p>
  
  <pre><code>package controllers
  
  import (
      "fmt"
      "go-testapp/app/models"
  
      _ "github.com/go-sql-driver/mysql"
      "github.com/jinzhu/gorm"
      "github.com/revel/revel"
  )
  
  var DB gorm.DB
  
  func InitDB() {
      var err error
      DB, err = gorm.Open("mysql", "root:@/go-testapp?charset=utf8&amp;parseTime=True")
      if err != nil {
          panic(err)
      }
      DB.LogMode(true)
      DB.AutoMigrate(models.User{})
  }
  
  type GormController struct {
      *revel.Controller
      DB *gorm.DB
  }
  </code></pre>
  
  <p><code>app/controller/app.go</code></p>
  
  <pre><code>package controllers
  
  import (
      "fmt"
      "go-bingo/app/models"
  
      _ "github.com/go-sql-driver/mysql"
      "github.com/revel/revel"
  )
  
  type App struct {
      GormController
  }
  
  func (c App) Index() revel.Result {
      user := models.User{Name: "Jinzhu", Age: 18}
  
      fmt.Println(c.DB)
      c.DB.NewRecord(user)
  
      c.DB.Create(&amp;user)
  
      return c.RenderJson(user)
  }
  </code></pre>
  
  <p>After running it results in: </p>
  
  <p><code>runtime error: invalid memory address or nil pointer dereference</code> on line 19 <code>c.DB.NewRecord(user)</code></p>
  
  <p>It successfully creates the datatables with automigrate, but I have no idea how I should use Gorm in my controller.</p>
  
  <p>Any hints in the right direction?</p>