Golang中国

我使用的是"github.com/astaxie/session"的session管理器,简单的写了下面一个demo:
usesession.go:

package main

import (
    "net/http"
    "github.com/astaxie/session"

    "fmt"
)

var globalSessions *session.Manager

func init() {
    globalSessions, _ = session.NewManager("memory","gosessionid",3600)
}

func getSession(w http.ResponseWriter, r *http.Request) {
    sess := globalSessions.SessionStart(w, r)
    session := sess.Get("username")
    fmt.Fprintf(w, "Get Session Ok, Session is %s", session)
}

func index(w http.ResponseWriter, r * http.Request) {
    sess := globalSessions.SessionStart(w, r)
    sess.Set("username", "set username")
    fmt.Fprint(w, "Set Session Ok!")
}

func main() {
    http.HandleFunc("/", index)
    http.HandleFunc("/get", getSession)
    err := http.ListenAndServe(":9090", nil)
    if err != nil {
        fmt.Println(err)
    }
}

运行后访问报错如下:

2017/06/05 18:46:58 http: panic serving 127.0.0.1:60154: runtime error: invalid memory address or nil pointer dereference
goroutine 5 [running]:
net/http.(*conn).serve.func1(0xc4200c8640)
    /usr/local/go/src/net/http/server.go:1721 +0xd0
panic(0x646ee0, 0x7bd200)
    /usr/local/go/src/runtime/panic.go:489 +0x2cf
github.com/astaxie/session.(*Manager).SessionStart(0x0, 0x79bae0, 0xc4200d2000, 0xc42000a800, 0x0, 0x0)
    /home/zhuxinquan/IdeaProjects/GoLanguage/GoSessionAndCookie/src/github.com/astaxie/session/session.go:60 +0x76
main.index(0x79bae0, 0xc4200d2000, 0xc42000a800)
    /home/zhuxinquan/IdeaProjects/GoLanguage/GoSessionAndCookie/src/usesession/usesession.go:23 +0x66
net/http.HandlerFunc.ServeHTTP(0x69f438, 0x79bae0, 0xc4200d2000, 0xc42000a800)
    /usr/local/go/src/net/http/server.go:1942 +0x44
net/http.(*ServeMux).ServeHTTP(0x7c6660, 0x79bae0, 0xc4200d2000, 0xc42000a800)
    /usr/local/go/src/net/http/server.go:2238 +0x130
net/http.serverHandler.ServeHTTP(0xc4200922c0, 0x79bae0, 0xc4200d2000, 0xc42000a800)
    /usr/local/go/src/net/http/server.go:2568 +0x92
net/http.(*conn).serve(0xc4200c8640, 0x79bf60, 0xc420016580)
    /usr/local/go/src/net/http/server.go:1825 +0x612
created by net/http.(*Server).Serve
    /usr/local/go/src/net/http/server.go:2668 +0x2ce
2017/06/05 18:46:58 http: panic serving 127.0.0.1:60156: runtime error: invalid memory address or nil pointer dereference
goroutine 18 [running]:
net/http.(*conn).serve.func1(0xc4200f8000)
    /usr/local/go/src/net/http/server.go:1721 +0xd0
panic(0x646ee0, 0x7bd200)
    /usr/local/go/src/runtime/panic.go:489 +0x2cf
github.com/astaxie/session.(*Manager).SessionStart(0x0, 0x79bae0, 0xc42010c000, 0xc420104000, 0x0, 0x0)
    /home/zhuxinquan/IdeaProjects/GoLanguage/GoSessionAndCookie/src/github.com/astaxie/session/session.go:60 +0x76
main.index(0x79bae0, 0xc42010c000, 0xc420104000)
    /home/zhuxinquan/IdeaProjects/GoLanguage/GoSessionAndCookie/src/usesession/usesession.go:23 +0x66
net/http.HandlerFunc.ServeHTTP(0x69f438, 0x79bae0, 0xc42010c000, 0xc420104000)
    /usr/local/go/src/net/http/server.go:1942 +0x44
net/http.(*ServeMux).ServeHTTP(0x7c6660, 0x79bae0, 0xc42010c000, 0xc420104000)
    /usr/local/go/src/net/http/server.go:2238 +0x130
net/http.serverHandler.ServeHTTP(0xc4200922c0, 0x79bae0, 0xc42010c000, 0xc420104000)
    /usr/local/go/src/net/http/server.go:2568 +0x92
net/http.(*conn).serve(0xc4200f8000, 0x79bf60, 0xc4200e6100)
    /usr/local/go/src/net/http/server.go:1825 +0x612
created by net/http.(*Server).Serve
    /usr/local/go/src/net/http/server.go:2668 +0x2ce

请指教!


zhuxinquan 于 2017-06-05 18:57 修改
3 回复
gogogolang
#1 gogogolang • 2017-06-06 10:18

说beego框架中的session更准确

gogogolang
#2 gogogolang • 2017-06-06 10:18

说beego框架中的session更准确

heimeil
#3 heimeil • 2017-06-06 11:12

globalSessions, _ = session.NewManager("memory","gosessionid",3600)
错误信息被忽略掉了

需要 登录 后方可回复, 如果你还没有账号你可以 注册 一个帐号。

作者

zhuxinquan
一直特立独行的朱

发表话题

作者其它话题