使用共享的会话变量从我的函数中插入mongodb

Hi i got the following document insert to work properly and i can call the function. However, i doubt that the DB connect stuff in this function is very efficient since i call it 30-40 times per minute.

i need to move the db session connect outside my function and i think its something with *mongoSession, but cant get it to work. Any help would be appriciated.

tldr: How do i move the connection outside the function

func insertmgo(aaa string, bbb string, ccc time.Time, wg *sync.WaitGroup) {

// mongo stuff
mongoDBDialInfo := &mgo.DialInfo{
    Addrs:    []string{MongoDBHosts},
    Timeout:  60 * time.Second,
    Database: AuthDatabase,
    Username: AuthUserName,
    Password: AuthPassword,
}
mongoSession, err := mgo.DialWithInfo(mongoDBDialInfo)
if err != nil {
    log.Fatalf("CreateSession: %s
", err)
}
mongoSession.SetMode(mgo.Monotonic, true)
c := mongoSession.DB("x").C("ships")

oneship.Created = ccc
oneship.Name = bbb
oneship.Type = aaa

c.Insert(oneship)
wg.Done()
}

found soloution

main {

 mongo stuff
    mongoDBDialInfo := &mgo.DialInfo{
        Addrs:    []string{MongoDBHosts},
        Timeout:  60 * time.Second,
        Database: AuthDatabase,
        Username: AuthUserName,
        Password: AuthPassword,
    }
    mongoSession, err := mgo.DialWithInfo(mongoDBDialInfo)
    if err != nil {
        log.Fatalf("CreateSession: %s
", err)
    }


}

call function with

fun(mongoSession, .....)

in your function

func fun(db *mgo.Session, .... , wg *sync.WaitGroup) {

that was is i think. not sure if there is other or better ways to do it, but this seem to work for me.