EDITED Solved: How should I create a singleton DBManager class in GoLang.
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
package dbprovider
import (
"github.com/jinzhu/gorm"
_"github.com/jinzhu/gorm/dialects/sqlite"
"rest/article"
"log"
)
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("sqlite3", "../articles.db")
if (err != nil) {
panic("Error initializing db")
} else {
log.Print("DB Initialized successfully")
}
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) > 0) {
err = errs[0]
} else {
log.Print("No error in this transactions")
}
}
return
}
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(..)
One way is to create an exported interface with the methods, and make the implementing type unexported. Create a global variable of the interface type, and initialize it with a package
init()
function. You don't need any synchronization as the packageinit()
function will run only once, safely.Package
init()
functions are executed once, automatically, by the runtime, before you could refer to anything from the package. For details, see Spec: Package initialization.For example:
Using it:
You could also do it without an
init()
function, e.g.:With this you may decide to make
newManager()
exported and users of your package could decide to use the sharedMgr
instance, or they could create anotherManager
, e.g. for testing purposes.Notes:
Mgr
is an exported global variable, and it is possible to assign a new value to it by other packages (e.g.dbprovider.Mgr = nil
). If you want to avoid this, you have to make it unexported, and provide a "getter" function for it, e.g.:And using it: