How to store and get a pointer reference in a global scope in GO

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

How to store and get a pointer reference in a global scope in GO

问题

我已经得到了以下代码:

  1. package main
  2. func main() {
  3. // 创建一个指向Mongo DB会话的指针引用
  4. session := mongoDB.CreateSession()
  5. // 问题1:如何在全局范围内存储指针引用,并在代码的任何地方使用它
  6. defer session.Close()
  7. // 注意:我假设代码调用处理程序方法,该方法调用控制器包中的Process方法(最后一个代码块)
  8. }

创建MongoDB会话的代码:

  1. package mongoDB
  2. func CreateSession() *mgo.Session {
  3. session, err := mgo.Dial("192.168.0.108:27017/databasename")
  4. if err != nil {
  5. panic(err)
  6. }
  7. session.SetMode(mgo.Monotonic, true)
  8. return session
  9. }

我想要在主函数中存储的指针引用的位置:

  1. package controller
  2. func Process() {
  3. // 问题2:如果可能的话,如何获取存储在问题1中的指针引用
  4. collection := mongoDB.CreateCollection(session, "namedatabase", "colectionData")
  5. mongoDB.InsertData(collection, "Ale", "45646565")
  6. }

我的理解是,你想避免在项目的所有函数中传递指针引用session

英文:

I have got the follow code:

  1. package main
  2. func main() {
  3. // create a pointer referece of session of Mongo DB
  4. session := mongoDB.CreateSession()
  5. // Question 1 : How to store a pointer reference in a global scope and using anywhere of the code
  6. defer session.Close()
  7. // Note I suppose that the code call to handler methods that call to the Process in the package controller(the last one code)
  8. }

Code of creating a session of MongoDB

  1. package mongoDB
  2. func CreateSession() *mgo.Session {
  3. session, err := mgo.Dial("192.168.0.108:27017/databasename")
  4. if err != nil {
  5. panic(err)
  6. }
  7. session.SetMode(mgo.Monotonic, true)
  8. return session
  9. }

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

  1. package controller
  2. func Process() {
  3. // Question 2 : How can a get the pointer reference store in Question 1 if is posible
  4. collection := mongoDB.CreateCollection(session, "namedatabase", "colectionData")
  5. mongoDB.InsertData(collection, "Ale", "45646565")
  6. }

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

得分: 0

你的controller包不能导入main包。这不是一个好主意(我相当确定这是不可能的)。但这并不意味着你不能在controller中有一个全局的session变量。

你可以尝试以下代码:

  1. package main
  2. import (
  3. "controller"
  4. "mongoDB"
  5. )
  6. func main() {
  7. // 创建Mongo DB会话的指针引用
  8. session := mongoDB.CreateSession()
  9. defer session.Close()
  10. controller.SetDBSession(session) // 或者使用controller.Init或其他你喜欢的方式
  11. controller.Process()
  12. }

然后在controller包中:

  1. package controller
  2. import "mongoDB"
  3. // 该包的全局session变量
  4. var session mongoDB.Session
  5. // SetDBSession将mongoDB会话设置为controller包要使用的会话。
  6. // 在调用Process()之前必须调用此函数。
  7. func SetDBSession(s mongoDB.Session) {
  8. session = s
  9. }
  10. func Process() {
  11. collection := mongoDB.CreateCollection(session, "namedatabase", "colectionData")
  12. mongoDB.InsertData(collection, "Ale", "45646565")
  13. }

使用这个解决方案,你只需要将会话传递给controller包一次,让main负责创建和关闭会话。

英文:

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:

  1. package main
  2. import (
  3. "controller"
  4. "mongoDB"
  5. )
  6. func main() {
  7. // create a pointer referece of session of Mongo DB
  8. session := mongoDB.CreateSession()
  9. defer session.Close()
  10. controller.SetDBSession(session) // Or controller.Init or whatever you like
  11. controller.Process()
  12. }

And then in the controller package you have:

  1. package controller
  2. import "mongoDB"
  3. // Global session var for the package
  4. var session mongoDB.Session
  5. // SetDBSession sets the mongoDB session to be used by the controller package.
  6. // This function must be called before calling Process()
  7. func SetDBSession(s mongoDB.Session) {
  8. session = s
  9. }
  10. func Process() {
  11. collection := mongoDB.CreateCollection(session, "namedatabase", "colectionData")
  12. mongoDB.InsertData(collection, "Ale", "45646565")
  13. }

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.

huangapple
  • 本文由 发表于 2014年4月23日 05:51:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/23230888.html
匿名

发表评论

匿名网友

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

确定