How to create singleton DB class in GoLang

☆樱花仙子☆ 提交于 2019-12-03 13:31:45

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 package init() 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:

package dbprovider

type Manager interface {
    AddArticle(article *article.Article) error
    // Add other methods
}

type manager struct {
    db *gorm.DB
}

var Mgr Manager

func init() {
    db, err := gorm.Open("sqlite3", "../articles.db")
    if err != nil {
        log.Fatal("Failed to init db:", err)
    }
    Mgr = &manager{db: db}
}

func (mgr *manager) AddArticle(article *article.Article) (err error) {
    mgr.db.Create(article)
    if errs := mgr.db.GetErrors(); len(errs) > 0 {
        err = errs[0]
    }
    return
}

Using it:

import "dbprovider"

if err := dbprovider.Mgr.AddArticle(someArticle); err != nil {
    // Handle error
}

You could also do it without an init() function, e.g.:

var Mgr = newManager()

func newManager() Manager {
    db, err := gorm.Open("sqlite3", "../articles.db")
    if err != nil {
        log.Fatal("Failed to init db:", err)
    }
    return &manager{db: db}
}

With this you may decide to make newManager() exported and users of your package could decide to use the shared Mgr instance, or they could create another Manager, 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.:

var mgr = newManager()

func Mgr() Manager { return mgr }

And using it:

err := dbprovider.Mgr().AddArticle(someArticle)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!