title: How can I share database connection between packages in Go?
body: <p>I declare my database connection in my <code>main</code> package, sort of like this:</p>
  
  <pre><code>package main
  
  var Db *sql.DB
  
  func main() {
      var err error
      db, err = sql.Open("postgres", "stuff...")
  
      // error handling and more app stuff
  }
  </code></pre>
  
  <p>To begin with I had all of my code in the <code>main</code> package and I could access the database connection from other files. However, I want to separate code into packages, and now the <code>Db</code> variable isn't visible anymore.</p>
  
  <p>Is it possible to share it across packages or do I have to add it as a parameter to whatever calls to other packages I have in my <code>main</code> function?</p>