At first, for the actual question, there is no way out of error checking.
Secondly, the correct way to use mgo is to have one sesson and clone it every time you need to do something, for example:
var ( mgoSession *mgo.Session ) func init() { sess, err := mgo.Dial("localhost") if err != nil { panic(err) // no, not really } mgoSession = sess } func do_stuff_with_mgo() { sess := mgoSession.Clone() defer sess.Close() //do stuff with sess } func main() { go do_stuff_with_mgo() go do_stuff_with_mgo() do_stuff_with_mgo() }
Also check out this article on mgo (I'm not an author, but it helped me learn mgo, it might be a bit dated, though.)
source share