dtv7174 2016-07-30 03:02
浏览 59
已采纳

如何在Go中将服务指针从一个包传递到另一个包?

I separated different http.HandleFunc in different files according to which page they are related to. I use gorilla/sessions to manage client sessions and user authentication and I use go-sql-driver for accessing a MySQL database.

Project Layout:

<PROJECT ROOT>
   -> <cliend>                     // This folder containing html, css, and js
   -> <pagehndler>
         -> <index>
                -> index.go        // Need to access database and session 
         -> <projbrwsr>
                -> projbrwsr.go    // Need to access database and session
   -> main.go

Therefore, I have 1 pointer pointing to the go-sql-driver service

db, err := sql.Open("mysql", "user:password@/dbname")

and 1 pointer pointing to gorilla/sessions service

var store = sessions.NewCookieStore([]byte("something-very-secret"))

There are 2 methods to pass the two pointers to other packages in my understanding:

  1. Wrap the two pointers into two packages (sess, db) and make them exported. And, the package that requires the service needed to import the packages (sess, db). And call the exported pointers.

    <PROJECT ROOT>
       -> <cliend>                     // This folder containing html, css, and js
       -> <pagehndler>
             -> <index>
                    -> index.go        // Need to access database and session 
             -> <projbrwsr>
                    -> projbrwsr.go    // Need to access database and session
       -> <service>
              -> sess.go               // Storing the database service pointer
              -> db.go                 // Storing the session service pointer
       -> main.go
    
  2. Initialize the two pointers in main package and pass them to the another package, that contain the page handle functions, as args. Inside the another package, set the args to a local variable so we can call it locally in the another package.

     <PROJECT ROOT>
        -> <cliend>                     // This folder containing html, css, and js
        -> <pagehndler>
              -> <index>
                     -> index.go        // Need to access database and session
                                        // Containing a func newService(db *DB)
              -> <projbrwsr>
                     -> projbrwsr.go    // Need to access database and session
                                        // Containing a func newService(sess *CookieStore)
        -> main.go
    

What is the best way to pass this two pointers to other packages for other handle function to call them?

  • 写回答

1条回答 默认 最新

  • doukuang1897 2016-07-30 08:44
    关注

    This is the concept which should work.

    Inside the package you should declare a variable, which is exported. This variable has to be a pointer.

    var myVar = 3
    var MyPointer = &myVar
    

    https://play.golang.org/p/EQDwGF7pjv

    From your main package you can set the pointer to your "global" db or session adress.

    mypackage.MyPointer = dbPointer
    

    Then the pointer inside your package points to your dbPointer. I think this is a good way to pass a pointer into other packages.

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

报告相同问题?

悬赏问题

  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能