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

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

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

  1. package main
  2. import(
  3. "./mylib"
  4. "net/http"
  5. )
  6. ...
  7. func main(){
  8. dbutil.MySession, err := dbutil.ConnectDb()
  9. if err != nil {
  10. panic(err)
  11. }
  12. // 为了方便起见,我将省略HTTP服务器配置代码
  13. http.HandleFunc(...)
  14. http.HandleFunc(...)
  15. }
  16. ...

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

  1. .
  2. ├── mylib
  3. └── dbutil.go
  4. └── server.go

dbutil.go中:

  1. package dbutil
  2. import (
  3. ...
  4. )
  5. var MySession *mgo.Session
  6. func ConnectDb() (*mgo.Session, error){
  7. session, err := mgo.Dial("127.0.0.1")
  8. if err != nil {
  9. return nil, err
  10. }
  11. return session, nil
  12. }

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

  1. # command-line-arguments
  2. ./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

  1. package main
  2. import(
  3. "./mylib"
  4. "net/http"
  5. )
  6. ...
  7. func main(){
  8. dbutil.MySession, err := dbutil.ConnectDb()
  9. if err != nil {
  10. panic(err)
  11. }
  12. // I will just omit the http server config code for convenience
  13. http.HandleFunc(...)
  14. http.HandleFunc(...)
  15. }
  16. ...

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:

  1. .
  2. ├── mylib
  3. │   └── dbutil.go
  4. └── server.go

And in dbutil.go:

  1. package dbutil
  2. import (
  3. ...
  4. )
  5. var MySession *mgo.Session
  6. func ConnectDb() (*mgo.Session, error){
  7. session, err := mgo.Dial("127.0.0.1")
  8. if err != nil {
  9. return nil, err
  10. }
  11. return session, nil
  12. }

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

  1. # command-line-arguments
  2. ./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

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

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

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

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

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

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

Just don't use the short variable declaration:

  1. var err error
  2. dbutil.MySession, err = dbutil.ConnectDb()
  3. // ...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:

确定