title: How do I connect database via ssh using gorm in golang?
body: <p>I am trying to connect to database via ssh, I am using the <code>database/sql</code> package to connect to the database, but I don't understand how to implement the <code>database/sql</code> to the gorm <code>jinzhu/gorm</code>. Can somebody tell me how to implement it? Or is there any way to connect by just using gorm package?
  I am fairly new to golang.</p>
  <p>This is what the code looks like.
  The main problem is in <code>NewBrandsRepository</code> method which cannot use type <code>*sql.DB</code> as <code>*gorm.DB</code></p>
  <pre><code>// Connect to the SSH Server
  sshcon, errSSH := ssh.Dial(&quot;tcp&quot;, fmt.Sprintf(&quot;%s:%d&quot;, sshHost, sshPort), sshConfig)
  if errSSH != nil {
  
  }
  
  defer sshcon.Close()
  
  // Now we register the ViaSSHDialer with the ssh connection as a parameter
  mysql.RegisterDial(&quot;mysql+tcp&quot;, (&amp;ViaSSHDialer{sshcon}).Dial)
  
  // And now we can use our new driver with the regular mysql connection string tunneled through the SSH connection
  db, errDB := sql.Open(&quot;mysql&quot;, fmt.Sprintf(&quot;%s:%s@mysql+tcp(%s)/%s&quot;, dbUser, dbPass, dbHost, dbName))
  
  if errDB != nil {
  
      fmt.Printf(&quot;Failed to connect to the db: %s\n&quot;, errDB.Error())
  
  }
  
  fmt.Printf(&quot;Successfully connected to the db\n&quot;)
  
  test := NewBrandsRepository(db)
  </code></pre>
  <p>and the <code>NewBrandsRepository</code> method is this</p>
  <pre><code>type brandsRepositoryImpl struct {
      db *gorm.DB
  }
  
  func NewBrandsRepository(db *gorm.DB) *brandsRepositoryImpl {
      return &amp;brandsRepositoryImpl{db}
  }
  </code></pre>