B
B
Bynov2017-02-18 22:05:53
go
Bynov, 2017-02-18 22:05:53

How to use a variable from main.go in another package?

Hello!
I just started learning Golang, I have a question.
I have 2 files in 2 different packages, simplified version below:
package main:

func main() {
        var globalSessions, _ = session.NewManager("memory", conf)
        go globalSessions.GC()
}

package admin:
func test(){
         sess, _ := globalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)
}

The question is, how to access the globalSessions variable declared in main?
I read the manuals, googled, somehow I can not find the answer. And I can't think of anything myself.
Thanks for the help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fadi Haj, 2017-02-19
@Bynov

So you are unlikely to succeed, but there is such an option:
admin.go:

var GlobalSessions *session.Manager

func test(){
    sess, _ := GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)
}

main.go:
func main() {
    var globalSessions, _ = session.NewManager("memory", conf)
    go globalSessions.GC()
    
    admin.GlobalSessions = globalSessions
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question