title: Golang / MGO -- panic: no reachable servers
body: <p>I have the following function that connects to Mongo.  For testing, I shutdown mongod, and want to allow the program to continue w/0 mongo if it's not available.  It seems that MGO throws a panic if the server can't be connected, so I wrote a defer/recover below, but the panic still causes the program to exit.  What's the proper way to recover from this?</p>
  
  <pre><code>func connectToMongo(sess *mgo.Session, coll *mgo.Collection, sessionErr error) bool {
      fmt.Println("enter main - connecting to mongo")
  
      // tried doing this - doesn't work as intended
      defer func() {
          if r := recover(); r != nil {
              var ok bool
              err, ok := r.(error)
              if !ok {
                  fmt.Printf("pkg:  %v,  error: %s", r, err)
              }
          }
          return false
      }()
  
      maxWait := time.Duration(5 * time.Second)
      sess, sessionErr = mgo.DialWithTimeout("localhost", maxWait)
      if sessionErr == nil {
          session.SetMode(mgo.Monotonic, true)
          coll = session.DB("MyDB").C("MyCollection")
      } else { // never gets here
          fmt.Println("Unable to connect to local mongo instance!")
      }
      return true
  }
  </code></pre>