错误:redisstor函数主体外部的非声明语句

时间:2019-08-07 17:01:51

标签: session go redis

跟随redisstor docs,我想修改this代码以使用Redis存储会话。这是我想出的:

.load(url)

但是我得到这个错误:

package session

import (
    "net/http"

    "github.com/gorilla/sessions"
    redisStore "gopkg.in/boj/redistore.v1"
)
var store *redisStore.RediStore
var Name string
var err error

store, err = redisStore.NewRediStore(10, "tcp", ":6379", "", []byte("secret-key"))
if err != nil  {
  log.Fatal("error getting redis store : ", err)
}
defer store.Close()

// Session stores session level information
type Session struct {
    Options   sessions.Options `json:"Options"`   
    Name      string           `json:"Name"`      
    SecretKey string           `json:"SecretKey"` 
}

// Configure the session cookie store
func Configure(s Session) {
    Store := store
    Store.Options = &s.Options
    Name = s.Name
}

// Instance returns a new session, never returns an error
func Instance(r *http.Request) *sessions.Session {
session, _ := Store.Get(r, Name)
return session
 }

我想知道这里出了什么问题,怎么解决?

2 个答案:

答案 0 :(得分:1)

在您的代码中

template<typename T>
void foo(T&& a) { doSomething(std::forward<T>(a)); /* a is lvalue -> matches 1. overload */ }

是非声明语句。它必须驻留在函数内部。例如store, err = redisStore.NewRediStore(10, "tcp", ":6379", "", []byte("secret-key")) if err != nil { log.Fatal("error getting redis store : ", err) } defer store.Close() init()函数。

答案 1 :(得分:1)

您可以将有问题的代码放在Configure函数中,以替换初始化CookieStore的原始代码。

var (
    // Store is the *redis* store
    Store *redisStore.RediStore
    // Name is the session name
    Name string
)

// ...

// Configure the session cookie store
func Configure(s Session) {
    var err error
    Store, err = redisStore.NewRediStore(10, "tcp", ":6379", "", []byte("secret-key"))
    if err != nil  {
        log.Fatal("error getting redis store : ", err)
    }

    Store.Options = &s.Options
    Name = s.Name
}