Web applications 跨多个包的全局会话管理的Go命名空间/范围问题

Web applications 跨多个包的全局会话管理的Go命名空间/范围问题,web-applications,go,global-variables,session-cookies,beego,Web Applications,Go,Global Variables,Session Cookies,Beego,首先让我说我是刚来戈朗的。现在已经和它一起工作了几个星期了。我真的很喜欢这种语言,但是 我在Golang的全局会话管理方面遇到了一些问题。我知道它是如何工作的,如果范围都在一个包中,我可以告诉我它是如何工作的,但是我最近刚刚为我的每个go文件创建了新的包。我这样做是因为我读到这是最佳实践,并且有利于重用 自从我将go文件移到它们自己的包而不是一个包中之后,会话管理就中断了。它希望每次都创建一个新会话,而不是重用现有会话。下面是一些代码,让您了解我在做什么: package main impor

首先让我说我是刚来戈朗的。现在已经和它一起工作了几个星期了。我真的很喜欢这种语言,但是


我在Golang的全局会话管理方面遇到了一些问题。我知道它是如何工作的,如果范围都在一个包中,我可以告诉我它是如何工作的,但是我最近刚刚为我的每个go文件创建了新的包。我这样做是因为我读到这是最佳实践,并且有利于重用

自从我将go文件移到它们自己的包而不是一个包中之后,会话管理就中断了。它希望每次都创建一个新会话,而不是重用现有会话。下面是一些代码,让您了解我在做什么:

package main

import (
    "net/http"
    "api/login"
    "api/globalsessionkeeper"
    "github.com/astaxie/beego/session"
)

func main() {
    http.HandleFunc("/login", login.DoLogin)

    og.Fatal(http.ListenAndServe(":8000", nil))
}

func Index(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
}

func init() {
    var err error
    fmt.Println("Session init")
    globalsessionkeeper.GlobalSessions, err = session.NewManager("mysql", `{"enableSetCookie":true, "SessionOn":true, "cookieName":"globalsession_id","gclifetime":120,"ProviderConfig":"root@tcp(172.16.0.23:3306)/databse"}`)
    if err != nil {
        fmt.Printf("Error")
    }
    globalsessionkeeper.GlobalSessions.SetSecure(true)
    go globalsessionkeeper.GlobalSessions.GC()
}
包globalsessionkeeper(我创建它只是为了在所有其他包中重用一个全局变量,例如“包登录”等等)

这是登录功能/软件包中的一个小漏洞。这是完美的工作与一切都在一个包:

if validated == true {
    //create session using the request data which includes the cookie/sessionid
    sessionStore, err := globalsessionkeeper.GlobalSessions.SessionStart(w, r)
    if err != nil {
        //need logging here instead of print
        fmt.Printf("Error, could not start session %v\n", err)
        break
    }
    defer sessionStore.SessionRelease(w) //update db upon completion for request

    if sessionStore.Get("uniquevalue") == nil {
        //need logging here instead of print
        fmt.Printf("uniquevalue not found, Saving Session, Get has %v\n", sessionStore)
        fmt.Printf("uniquevalue not found, Saving Session, Get has %v\n", sessionStore.Get("uniquevalue"))
        err = sessionStore.Set("uniquevalue", input.uniquevalue)
        if err != nil {
            //need logging here instead of print
            fmt.Printf("Error while writing to DB, %v\n", err)
            break
        }
    } else {
        //need logging here instead of print
        fmt.Printf("Found Session! Session uniquevalue = %v\n", sessionStore.Get("uniquevalue"))
    }
    //Send back 204 no content (with cookie)
    w.WriteHeader(http.StatusNoContent)
} else {
    fmt.Printf("Login Failed")
    w.WriteHeader(http.StatusUnauthorized)
}
数据库具有正确的条目。它具有为其创建的每个会话存储的唯一值。这里还有一些输出:

answer = true
uniquvalue not found, Saving Session, Get has &{0xc208046b80 f08u0489804984988494994 {{0 0} 0 0 0 0} map[]}
uniquevalue not found, Saving Session, Get has <nil>
2015/06/06 17:32:26 http: multiple response.WriteHeader calls
answer=true
找不到uniquvalue,正在保存会话,Get具有&{0xc208046b80 f08u0489804984988494994{{0}0}映射[]
未找到uniquevalue,正在保存会话,Get已
2015/06/06 17:32:26 http:multipleresponse.WriteHeader调用
当然,multipleresponse.WriteHeader是我必须纠正的另一个问题。go例程最初发送的是200OK,但我想将其更改为204 no内容,但一旦我发送了,它就开始给我这个错误


任何帮助都将不胜感激。谢谢

结果证明这一直都在起作用。我将错误的cookie值传递回api。愚蠢的错误会引起各种各样的头痛。

为什么不使用比戈的路线系统呢?您可以直接通过上下文对象访问会话。无需创建全局会话包。“为每个我的go文件创建了新包”。我希望这是一个输入错误,您并不是真的将每个
*.go
文件放在它自己的包中。仅在合理的情况下使用包,也就是说,它是一个具有明确API的独立概念,并且/或者其他人希望/需要使用包是合理的。另外,
globalsessionkeeper
是一个蹩脚的非惯用语。你能解释一下吗?如果我有一个登录名和一个注册文件,它们不应该在单独的包中吗?对不起,我是刚来戈朗的。在我走的时候想弄清楚。
answer = true
uniquvalue not found, Saving Session, Get has &{0xc208046b80 f08u0489804984988494994 {{0 0} 0 0 0 0} map[]}
uniquevalue not found, Saving Session, Get has <nil>
2015/06/06 17:32:26 http: multiple response.WriteHeader calls