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

Follow redisstor docs, I'd like to modify this code to use redis to store sessions. Here is what I came up with:

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
 }

But I get this error:

vendor/app/shared/session/session.go:19:1: syntax error: non-declaration statement outside function body

I'm wondering what is wrong here and how can I fix it?

You can put the offending code inside the Configure function to replace the original that initializes the 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
}

In your code

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

are non declaration statements. This must reside inside a function. For example init() or main() functions.