Skip to content

Instantly share code, notes, and snippets.

@hfogelberg
Last active September 19, 2017 08:54
Show Gist options
  • Save hfogelberg/cc44a2e53d133de585b77dc3aed4ae15 to your computer and use it in GitHub Desktop.
Save hfogelberg/cc44a2e53d133de585b77dc3aed4ae15 to your computer and use it in GitHub Desktop.
Create Mongo connection from Go
package main
import "gopkg.in/mgo.v2"
type Connection struct {
DB *mgo.Database
}
const MongoDb = "test"
func main() {
host := os.Getenv("MONGO_DB_HOST")
session, err := mgo.Dial(host
if err != nil {
log.Printf("Cannot connect to Db %s\n", err.Error())
return
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
connection := Connection{session.DB(MognoDb)}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment