gpt4 book ai didi

session - 错误 : non-declaration statement outside function body on redisstor

转载 作者:可可西里 更新时间:2023-11-01 11:50:32 25 4
gpt4 key购买 nike

关注rediststor docs , 我想修改 this使用redis存储 session 的代码。这是我想出的:

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
}

但是我得到这个错误:

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

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

最佳答案

您可以将有问题的代码放在 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
}

关于session - 错误 : non-declaration statement outside function body on redisstor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57399221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com