Sharing data across packages/go files

3,615 views
Skip to first unread message

Kim Eik

unread,
Nov 10, 2012, 2:02:06 PM11/10/12
to golan...@googlegroups.com
I`m learning GO by developing a web application with MongoDB as backend. And i seem to have come across a pretty basic issue (im guessing).

In my main method i`m trying to create a public/exported variable named Session. This works if im trying to get the Session var from a different go file as long as it is in the same folder as the main go file.

var Session *mgo.Session                                                                                                    

func main() {                                                                                                                                        
                                                                                                                                                                                                                           
        Session, err = mgo.Dial("localhost")                                                                                                       
        if err != nil {                                                                                                                           
                panic(err)
        }  
}

How should i go about sharing the mgo session? so they can be reached from other go files residing in other directories?

Much appreciated
Kim

stevewang

unread,
Nov 10, 2012, 2:44:16 PM11/10/12
to golan...@googlegroups.com
  1. package otherpackage
  2. import "labix.org/v2/mgo"
  3. func ReceiveSession(session *mgo.Session) {
  4.     //...
  5. }

  6. package main
  7. import "labix.org/v2/mgo"
  8. import "otherpackage"
  1. var Session *mgo.Session
  2. func main() {
  1.     //...
  2.     otherpackage.ReceiveSession(Session)
  3.     //...
  4. }
Is this you want?

Kim Eik

unread,
Nov 10, 2012, 3:04:56 PM11/10/12
to stevewang, golan...@googlegroups.com
No, not really, as i need the session in a http handler function. The handler function only accepts w ResponseWriter, req *Request. Now, if there was only some way of expanding this to include a context object that could hold a mgo session, then that would be great. But as far as i know, i don't think this is possible. At least not directly.

However, i could probably create my own handler object that just proxies the original handler and supplies a context. But im not sure of how to proceed with this.

Is there any other ways of getting the session from the handler function, instead of trying to inject it somehow?


--
 
 

stevewang

unread,
Nov 10, 2012, 3:21:51 PM11/10/12
to golan...@googlegroups.com, stevewang
I don't fully understand your requirements.
And I don't know the relations between your packages.
Can't you import mgo in the package where you define your http handler function?

Patrick Mylund Nielsen

unread,
Nov 10, 2012, 3:23:25 PM11/10/12
to Kim Eik, stevewang, golan...@googlegroups.com
When exporting Session, you can initialize it in init() in the first package, then use it in others, but that is a bad way to structure your application, IMO. If another package is using the session in your first package, they probably shouldn't be separate packages, and if they are, they should take the session as a parameter instead.

As for adding a context to a request, I usually set up a default handler with http.Serve("/", foo), set up the context in foo, and then call the respective function (which takes the request and context as parameters) via my own muxer or gorilla/mux.


--
 
 

Rémy Oudompheng

unread,
Nov 10, 2012, 3:43:23 PM11/10/12
to Kim Eik, stevewang, golan...@googlegroups.com
On 2012/11/10 Kim Eik <k...@heldig.org> wrote:
> No, not really, as i need the session in a http handler function. The
> handler function only accepts w ResponseWriter, req *Request. Now, if there
> was only some way of expanding this to include a context object that could
> hold a mgo session, then that would be great. But as far as i know, i don't
> think this is possible. At least not directly.
>
> However, i could probably create my own handler object that just proxies the
> original handler and supplies a context. But im not sure of how to proceed
> with this.
>
> Is there any other ways of getting the session from the handler function,
> instead of trying to inject it somehow?

You can use a net/http.Handler
(http://golang.org/pkg/net/http/#Handler) or a closure.

For example, you other package could do:

func HandleHTTP(ses *mgo.Session) {
http.HandleFunc("/my/path", func(w http.ResponseWriter, req *http.Request) {
DoSomethingWithSessionAndRequest(ses, w, req)
})
}

Or if you need a handler factory:

type myHandler struct {
Session *mgo.Session
}

func (h myHandler) Handle(w http.ResponseWriter, req *http.Request) {
blah blah
}

func MakeHTTPHandler(ses *mgo.Session) {
return myHandler{Session: ses}
}

Rémy.

Luis Furquim

unread,
Nov 11, 2012, 12:10:12 AM11/11/12
to golan...@googlegroups.com
Hello

I don't know if it works, but, as I understood, it appears that
you want something like this:

package sessionpackage


var Session *mgo.Session



package otherpackage
import "sessionpackage"

    //...
    somefunc(sessionpackage.Session)
    //...


package main
import "sessionpackage"


func main() {
    //...
    sessionpackage.Session, err = mgo.Dial("localhost")                                                                                                       
        if err != nil {                                                                                                                           
                panic(err)
        }  
    //...
}


Best regards
Luis Otavio de Colla Furquim
Reply all
Reply to author
Forward
0 new messages