title: How to deal with unknown variables or How to deal with multiple databases
body: <p>I'm working on a Go RESTful API application with multiple databases. When starting the server, the user supplies which database they would like to use.</p>
  
  <p>In the application, I have <strong>three functions</strong> of which one handles the connection: <code>selectedDb.Get()</code>, <code>selectedDb.Add()</code>, <code>selectedDb.Connect()</code>.</p>
  
  <p>If somebody selects, Mysql, it handles things for Mysql, if someone selects MongoDB it handles things for Mongo and so on.</p>
  
  <p>This is how I <em>try</em> to accomplish this:</p>
  
  <p><strong>DbInterface.go</strong></p>
  
  <pre><code>package dbinit
  
  type Object struct {
      Uuid         string
      Object       string
      Deleted      bool
  }
  
  // The interface that all connectors should have
  type Intfc interface {
      Connect() HERE_LIES_MY_PROBLEM
      Add(string, string, string) string
      Get(string) (Object, bool)
  }
  </code></pre>
  
  <p><strong>MySQL.go</strong></p>
  
  <pre><code>package mysqlConnector
  
  import (
      ...
  )
  
  type Mysql struct{}
  
  // Connect to mysql
  func (f Mysql) Connect() HERE_LIES_MY_PROBLEM {
  
      client, err = sql.Open("mysql", "yourusername:yourpassword@/yourdatabase")
      if err != nil {
          panic(err.Error())    
      }
  
      return client
  }
  
  // Add item to DB
  func (f Mysql) Add(owner string, refType string, object string) string {
      // do stuff related to this DB
  
      return // a string
  }
  
  func (f Mysql) Get(Uuid string) (dbinit.Object, bool) {
      // do stuff related to this DB
  
      return // an object and a bool
  }
  </code></pre>
  
  <p><strong>Mongo.go</strong></p>
  
  <pre><code>package mongoConnector
  
  import (
      ...
  )
  
  type Mongo struct{}
  
  // Connect to mongo
  func (f Mongo) Connect() HERE_LIES_MY_PROBLEM {
  
      info := &amp;mgo.DialInfo{
          Addrs:    []string{hosts},
          Timeout:  60 * time.Second,
          Database: database,
          Username: username,
          Password: password,
      }
  
      client, err := mgo.DialWithInfo(info)
      if err != nil {
          panic(err)
      }
  
      return client
  }
  
  // Add item to DB
  func (f Mongo) Add(owner string, refType string, object string) string {
      // do stuff related to this DB
  
      return // a string
  }
  
  func (f Mongo) Get(Uuid string) (dbinit.Object, bool) {
      // do stuff related to this DB
  
      return // an object and a bool
  }
  </code></pre>
  
  <p><strong>main.go</strong></p>
  
  <pre><code>...
  
  var selectedDb dbinit.Intfc
  
  commandLineInput := "mysql" // just for the example
  
  if commandLineInput == "mysql" {
      selectedDb = mysqlConnector.Mysql{}
  } else if commandLineInput == "mongo" {
      selectedDb = mongoConnector.Mongo{}
  }
  
  client := selectedDb.Connect()
  
  // this runs everytime the API is called
  api.HandlerFoobar = foobar.handlerFunction(func(params foobar.Params) middleware.Responder {
  
      // Here I want to add something to the selected dbinit
      selectedDb.Get(client, addStringA, addStringB, addStringC)
  
      return // the API response
  
  })
  
  ...
  </code></pre>
  
  <p><strong>Problem statement</strong></p>
  
  <p>When I return the client for Mysql, it doesn't work for Mongo and visa versa.</p>
  
  <p>I want to connect to the database ONLY when starting the server and store the <code>client</code> inside the client variable. The problem, however, is that Mongo returns another client than Mysql does and so forth.</p>
  
  <ol>
  <li>What should be in the places where I have <code>HERE_LIES_MY_PROBLEM</code> in the code?</li>
  <li>Or do I get the Go paradigm wrong for dealing with these things?</li>
  </ol>