dongrui6787 2014-04-22 21:51
浏览 59
已采纳

如何在GO的全局范围内存储和获取指针引用

I have got the follow code:

package main


func main() {
    // create  a pointer referece of session of Mongo DB
session := mongoDB.CreateSession()

    // Question 1 : How to store a pointer reference in a global scope and using anywhere of the code  

defer session.Close()

    // Note I suppose that the code call to handler methods that call to the Process in the package controller(the last one code)

}

Code of creating a session of MongoDB

package mongoDB


func CreateSession() *mgo.Session {
session, err := mgo.Dial("192.168.0.108:27017/databasename")
if err != nil {
    panic(err)
}
session.SetMode(mgo.Monotonic, true)
return session
}

Place where I want to use the pointer reference that was store in the main

package controller


func Process()  {

    // Question 2 : How can a get the pointer reference store in Question 1 if is posible
collection := mongoDB.CreateCollection(session, "namedatabase", "colectionData")
mongoDB.InsertData(collection, "Ale", "45646565")

}

The idea is to avoid passing by reference session(my pointer reference in the main function) in every one of the functions created for all the project.

  • 写回答

1条回答 默认 最新

  • douji5746 2014-04-23 01:27
    关注

    You can't have your controller package import main. That is not a good idea (and I am quite sure it is not even possible). But that doesn't mean you can't have a global session variable in the controller.

    You can try this:

    package main
    
    import (
        "controller"
        "mongoDB"
    )
    
    func main() {
        // create  a pointer referece of session of Mongo DB
        session := mongoDB.CreateSession()
        defer session.Close()
    
        controller.SetDBSession(session) // Or controller.Init or whatever you like
    
        controller.Process()
    }
    

    And then in the controller package you have:

    package controller
    
    import "mongoDB"
    
    // Global session var for the package
    var session mongoDB.Session
    
    // SetDBSession sets the mongoDB session to be used by the controller package.
    // This function must be called before calling Process()
    func SetDBSession(s mongoDB.Session) {
        session = s
    }
    
    func Process() {
        collection := mongoDB.CreateCollection(session, "namedatabase", "colectionData")
        mongoDB.InsertData(collection, "Ale", "45646565")
    }
    

    Using this solution, you will only have to pass the session to the controller package once, letting main take care of the creating and closing of the session.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line