代码之家  ›  专栏  ›  技术社区  ›  markhorrocks

运行时错误:内存地址无效或无指针取消引用,请再次执行[重复]

go
  •  -3
  • markhorrocks  · 技术社区  · 6 年前

    我正在尝试使用mysqlstore后端包在Golang Web应用程序中进行Gorilla会话。我在跟随 this example 我的密码是一样的。代码构建和运行良好,但当我在浏览器中转到localhost:8080/时,我会收到此错误。

    运行时错误:内存地址无效或无指针取消引用

    这是我的代码:

    package main
    
      import (
        "fmt"
        "github.com/srinathgs/mysqlstore"
        "net/http"
      )
    
      var store *mysqlstore.MySQLStore
    
      func sessTest(w http.ResponseWriter, r *http.Request) {
        session, err := store.Get(r, "foobar")
        session.Values["bar"] = "baz"
        session.Values["baz"] = "foo"
        err = session.Save(r, w)
        fmt.Printf("%#v\n", session)
        fmt.Println(err)
      }
    
    func main() {
    
        store, err := mysqlstore.NewMySQLStore("root:mypass@tcp(127.0.0.1:3306)/mydb?parseTime=true&loc=Local", "sessions", "/", 3600, []byte("<SecretKey>"))
        if err != nil {
          panic(err)
        }
        defer store.Close()
    
            http.HandleFunc("/", sessTest)
            http.ListenAndServe(":8080", nil)
    }
    

    以下是完整的错误消息:

    2019/02/12 02:46:43 http:panic-serving[::1]:63119:运行时错误: 内存地址无效或无指针取消引用goroutine 34 [运行]:net/http.(*conn).serve.func1(0xc00112320) /usr/local/酒窖/go/1.11.5/libexec/src/net/http/server.go:1746+0xd0 紧急(0x12C9F40、0x1650950) /usr/local/cell/go/1.11.5/libexec/src/runtime/panic.go:513+0x1B9 github.com/srinathgs/mysqlstore.(*mysqlstore).new(0x0,0xc00138000,新 0x1324045、0x6、0x158afa0、0xc0010a000、0xb) /用户/mark/go/src/github.com/srinathgs/mysqlstore/mysqlstore.go:137 +0xef github.com/gorilla/sessions.(*注册表).get(0xc0011e0e0,0x1376e20,0x0,0x1324045,0x6,0xc0010c100,0x0,0x1) /用户/mark/go/src/github.com/gorilla/sessions/sessions.go:139+0x142 github.com/srinathgs/mysqlstore.(*mysqlstore).get(0x0,0xc00138000, 0x1324045、0x6、0x1086222、0xC00010A044、0xC00010A050) /用户/mark/go/src/github.com/srinathgs/mysqlstore/mysqlstore.go:131 +0x63主.sestest(0x13770a0,0xc0013c000,0xc00138000)/用户/mark/go/src/testsessions/main.go:12+0x61 net/http.handlerFunc.servehtp(0x133A680、0x13770A0、0xC00013C000, 0xC000 0138000)

    <gt;

    1 回复  |  直到 6 年前
        1
  •  5
  •   pkm    6 年前

    这个 store 您在主功能中创建的未分配给全局 商店 . 处理程序中使用的全局存储仍然为零。这是因为 := 运算符起作用,并且您正试图分配给在其他地方声明的var。

    你也可以

    1. 通过不使用 = 声明 var err error 在那条线上 例如
    var err error
    store, err = mysqlstore.NewMySQLStore(...
    
    1. 或者我推荐的方法(没有全局变量)是:初始化 商店 和您所做的一样,还可以通过将处理程序函数包装在一个闭包中并将存储传递到该闭包中来初始化处理程序。

    例如

    package main
    
    import (
        "fmt"
        "github.com/srinathgs/mysqlstore"
        "net/http"
    )
    
    // note: this returns a http.HandlerFunc, which works because
    // http.HandlerFunc is just a named type for a function which accepts http.ResponseWriter and *http.Request as args
    // see the docs at https://golang.org/pkg/net/http/#HandlerFunc
    // (but yea at the end of the day, this is a function which returns another function)
    func makeSessTest(store *mysqlstore.MySQLStore) http.HandlerFunc {
        return func(w http.ResponseWriter, r *http.Request) {
            session, err := store.Get(r, "foobar")
            session.Values["bar"] = "baz"
            session.Values["baz"] = "foo"
            err = session.Save(r, w)
            fmt.Printf("%#v\n", session)
            fmt.Println(err)
        }
    }
    
    func main() {
        store, err := mysqlstore.NewMySQLStore("root:mypass@tcp(127.0.0.1:3306)/mydb?parseTime=true&loc=Local", "sessions", "/", 3600, []byte("<SecretKey>"))
        if err != nil {
            panic(err)
        }
        defer store.Close()
    
        http.HandleFunc("/", makeSessTest(store))
        http.ListenAndServe(":8080", nil)
    }