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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序