title: GORM DB Connection on other package
body: <p>I start learning Go, reading about pointers, and want to split my database connection , and handler function for API. Already tried myself, by following this <a href="https://stackoverflow.com/questions/50751788/how-to-import-gorm-db-connection-from-another-file-or-package">solution</a> , but when i trying to read data, i am having this error </p>
  
  <p><strong>[2018-06-26 21:59:45]  sql: database is closed</strong>
  this is my source code.</p>
  
  <p>db.go</p>
  
  <pre><code>package db
  
  import (
  "fmt"
  
  "github.com/jinzhu/gorm"
  "github.com/joho/godotenv"
  
  "os"
  )
  
  var Db *gorm.DB
  
  func Open() error {
  
    var err error
    _ = godotenv.Load(".env")
  
    dbType := os.Getenv("DB_TYPE")
    dbConnString := os.Getenv("DB_CONN_STRING")
  
    Db, err = gorm.Open(dbType, dbConnString)
    if err != nil {
      fmt.Println(err)
    }
    Db.LogMode(true)
    defer Db.Close()
  
    return err
  }
  
  func Close() error {
      return Db.Close()
  }
  </code></pre>
  
  <p>person.go</p>
  
  <pre><code>package model
  
  import (
      "github.com/jinzhu/gorm"
      "github.com/gin-gonic/gin"
      "fmt"
      "namastra/gin/result"
      "namastra/gin/db"
  )
  
  type Person struct {
      gorm.Model
      FirstName string `json:”firstname”`
      LastName  string `json:”lastname”`
  }
  
  /*var db *gorm.DB
  var err error*/
  
  func GetPeople(c *gin.Context) {
  
      var people []result.Person
      if err := db.Db.Select("ID,first_name,last_name").Find(&amp;people).Error; err != nil {
          c.AbortWithStatus(404)
          fmt.Println(err)
      } else {
          c.JSON(200, people)
      }
  }
  </code></pre>
  
  <p>main.go</p>
  
  <pre><code>package main
  
  import (
      "log"
      "namastra/gin/handler"
      "namastra/gin/model"
      "net/http"
      "time"
  
      "github.com/adam-hanna/jwt-auth/jwt"
      "github.com/gin-gonic/gin"
      _ "github.com/jinzhu/gorm/dialects/postgres"
  
      "namastra/gin/db"
  )
  
  func main() {
  if err := db.Open(); err != nil {
      // handle error
      panic(err)
  }
  defer db.Close()
  
  router := gin.Default()
  router.Use(gin.Recovery())
  private := router.Group("/auth")
  
  ....(ommited)
  
  router.GET("/", gin.WrapH(regularHandler))
  router.GET("/people/", model.GetPeople)
  router.Run("127.0.0.1:3000")
  }
  </code></pre>
  
  <p>Sorry for my bad english, any kind of help is appreciated.</p>
  
  <p>thank you.</p>
  
  <p>edit1: <strong>case closed.</strong></p>
  
  <p>solution is by removing </p>
  
  <pre><code>defer Db.Close()
  </code></pre>
  
  <p>from db.go.</p>
  
  <p>edi2: <strong>update some knowledge i learn by working in go project</strong></p>