1234567891011121314151617181920212223242526 |
- package clinit
- import (
- "fmt"
- "gopkg.in/mgo.v2"
- "os"
- "strings"
- "time"
- )
- var MongoSession *mgo.Session
- func InitMongo(address, username, password string) {
- addressList := strings.Split(address, ",")
- dialInfo := &mgo.DialInfo{Addrs: addressList, Username: username, Password: password, Timeout: time.Second * 60}
- session, err := mgo.DialWithInfo(dialInfo)
- if err != nil {
- fmt.Printf("new mongo client failed. error:%s\n", err)
- os.Exit(1)
- }
- MongoSession = session
- }
- func GetMongoSession() *mgo.Session {
- return MongoSession.Clone()
- }
|