title: How to share mysql connection between http goroutines?
body: <p>I'm a Go noob and can't find any complete examples of opening a mysql connection in Go and then sharing it among http handlers. Here is my code so far, how would I use the db connection that I opened in main() in my HomeHandler? </p>
  
  <pre><code>package main
  
  import (
    "database/sql"
    "fmt"
    _ "github.com/go-sql-driver/mysql"
    "github.com/gorilla/mux"
    "log"
    "net/http"
  )
  
  func main() {
  
    fmt.Println("starting up")
  
    db, err := sql.Open("mysql", "root:@/mydb?charset=utf8")
    if err != nil {
      log.Fatalf("Error opening database: %v", err)
    }
  
    db.SetMaxIdleConns(100)
  
    r := mux.NewRouter()
    r.HandleFunc("/", HomeHandler)
  
    http.Handle("/", r)
    http.ListenAndServe(":8080", nil)
  
  }
  
  func HomeHandler(w http.ResponseWriter, r *http.Request) {
  
    fmt.Fprintf(w, "home")
  
  }
  </code></pre>