title: How to create singleton DB instance
body: <p>I referred a few code samples of how to create go singleton but I wish to have methods in those and call them on their singleton reference. My Code is as follows</p>
  <pre><code>package dbprovider
  
  import (
      &quot;github.com/jinzhu/gorm&quot;
      _&quot;github.com/jinzhu/gorm/dialects/sqlite&quot;
      &quot;rest/article&quot;
      &quot;log&quot;
  )
  
  type DBOperations interface {
      AddArticle(article *article.Article)
  }
  
  type DBManager struct {
      db            *gorm.DB
      isInitialized bool
  }
  
  var dbManagerInstance = new()
  
  func GetDBManager() DBManager {
      return dbManagerInstance
  }
  
  func new() DBManager {
      localDbRef, err := gorm.Open(&quot;sqlite3&quot;, &quot;../articles.db&quot;)
      if (err != nil) {
          panic(&quot;Error initializing db&quot;)
      } else {
          log.Print(&quot;DB Initialized successfully&quot;)
      }
      return DBManager{db:localDbRef, isInitialized:true}
  }
  
  func (dbManager DBManager)  AddArticle(article article.Article) (err error) {
      if (dbManager.isInitialized) {
          tx := dbManager.db.Begin()
          //dbManager.db.NewRecord(article)
          //dbManager.db.Commit()
          tx.NewRecord(article)
          tx.Commit()
          errs := dbManager.db.GetErrors()
          if (len(errs) &gt; 0) {
              err = errs[0]
          } else {
              log.Print(&quot;No error in this transactions&quot;)
          }
  
      }
      return
  }
  </code></pre>
  <p><strong>With a new answer I have updated this question including answer. But I have a few queries. How to cathc and return exception from gorm.Create(..)</strong></p>