Using and assign variables of other file (package) in go

huangapple go评论81阅读模式
英文:

Using and assign variables of other file (package) in go

问题

我有一个使用MongoDB作为后端数据库的HTTP服务器,我将数据库操作封装在一个单独的文件(package)中,并且不想每次都建立连接。所以我想创建一个全局会话,并像这样那样复制它可能是个好主意。到目前为止,我已经完成了以下工作:

server.go文件中,我启动了HTTP服务器,并且我还想从这里初始化MongoDB连接,因为到目前为止,我不知道其他可以在整个HTTP服务器生命周期中保持连接的方法 Using and assign variables of other file (package) in go

package main

import(
    "./mylib"
    "net/http"
)
...
func main(){
    dbutil.MySession, err := dbutil.ConnectDb()
    if err != nil {
       panic(err)
    }
    // 为了方便起见,我将省略HTTP服务器配置代码
    http.HandleFunc(...)
    http.HandleFunc(...)
}
...

这里是dbutil.go文件,其中包含变量MySession。顺便说一下,它们的总目录结构应该是这样的:

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

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
}

但是当我编译并运行服务器时,它报错:

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

我注意到,如果我将dbutil.MySession, err := dbutil.ConnectDb()更改为dbutil.MySession, err = dbutil.ConnectDb(),事情也不会起作用。

那么,在这种情况下,我该如何分配全局变量MySession?或者我一开始做错了什么(尝试使用其他文件的变量)?

英文:

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 Using and assign variables of other file (package) in go

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?

答案1

得分: 4

短变量声明 := 创建新变量。新变量的名称不能包含包名,但你的代码中包含了包名:

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

只需不使用短变量声明即可:

var err error
dbutil.MySession, err = dbutil.ConnectDb()
// ...你的代码的其余部分
英文:

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

huangapple
  • 本文由 发表于 2015年2月27日 21:41:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/28766489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定