V
V
Vladimir Grabko2016-06-28 16:23:49
go
Vladimir Grabko, 2016-06-28 16:23:49

How to implement polyphorism in Go?

I have the nsql abstract package. I want to make drivers for different nosql databases. (ala analogue of the database/sql package only for nosql). How to do it right in general?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Semchenko, 2016-06-28
@VGrabko

Why if there is?
https://github.com/avelino/awesome-go#database-drivers

func InitDatabase(driver, dbPath string) error {
  var db gorm.DB
  var err error

  switch driver {
  case "sqlite3":
    db, err = gorm.Open("sqlite3", dbPath)
    if err != nil {
      return err
    }
    break
  case "mysql":
    db, err = gorm.Open("mysql", dbPath)
    if err != nil {
      return err
    }
    break
  case "postgres":
    db, err = gorm.Open("postgres", dbPath)
    if err != nil {
      return err
    }
    break
  default:
    return fmt.Errorf("Unknown database driver of %v", driver)
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question