在go中使用和分配其他文件(包)的变量

I have a http server using mongodb as a backend db, I wrapped the db operations in a separate file (package), and I don't want to make a connection every time, So I think make a global session and copy it like this maybe a good idea. So this is so far I've got:

File server.go in which I start the http server, and I also want to init the mongodb connection from here, for I don't know other way to make a connection that could live through the whole life of the http server so far :-(

package main

import(
    "./mylib"
    "net/http"
)
...
func main(){
    dbutil.MySession, err := dbutil.ConnectDb()
    if err != nil {
       panic(err)
    }
    // I will just omit the http server config code for convenience
    http.HandleFunc(...)
    http.HandleFunc(...)
}
...

And here is the file dbutil.go which contains the variable MySession. By the way, the total dir structure of them is like this, should be fine:

.
├── mylib
│   └── dbutil.go
└── server.go

And in dbutil.go:

package dbutil
import (
  ...
)

var MySession *mgo.Session
func ConnectDb() (*mgo.Session, error){
   session, err := mgo.Dial("127.0.0.1")
   if err != nil {
       return nil, err
   }
   return session, nil
}

But when I compile them and run the server, it complains that:

# command-line-arguments
./server.go:28: cannot declare name dbutil.MySession

I notice that if I change dbutil.MySession, err := dbutil.ConnectDb() to dbutil.MySession, err = dbutil.ConnectDb() things will not work either.

So How Can I assign the global Variable MySession in this case? Or did I do something wrong (try to use a variable of other file) at the first place?

The Short variable declaration := creates new variables. The name of the new variables cannot contain package names, but yours does:

dbutil.MySession, err := dbutil.ConnectDb()

Just don't use the short variable declaration:

var err error
dbutil.MySession, err = dbutil.ConnectDb()
// ...rest of your code