dongya6381
2019-01-03 19:01
浏览 209
已采纳

如何在Golang中以不同的软件包使用Gorilla Sessions

I have main package (main.go):

package main

import (
  "github.com/gorilla/sessions"
  ...
)

func main() {
  var store = sessions.NewCookieStore([]byte("secret"))

  http.HandleFunc("/", routes.Index)

  http.ListenAndServe(":8080", nil)
  ...

And I have another package (index.go):

package routes

import (
  "github.com/gorilla/sessions"
  ..
)

func Index(res http.ResponseWriter, req *http.Request) {
  session, _ := store.Get(req, "session-name")
  ...

How can I get session value from another package? Or should I pass it to my Handler (if yes how to do it?).

I am new in Golang. Please, help.

图片转代码服务由CSDN问答提供 功能建议

我有主软件包(main.go):</ p>

  包main 
 
import(
“ github.com/gorilla/sessions"
 ... 
)
 
func main(){
 var store = session.NewCookieStore([] byte(” secret“  ))
 
 http.HandleFunc(“ /”,route.Index)
 
 http.ListenAndServe(“:8080”,nil)
 ... 
 </ code> </ pre> 
  
 

我还有另一个包(index.go):</ p>

 包路由
 
导入(
“ github.com/gorilla/sessions"
  .. 
)
 
func索引(res http.ResponseWriter,req * http.Request){
会话,_:= store.Get(req,“会话名称”)
 ... 
 <  / code> </ pre> 
 
 

如何从另一个包中获取会话值? 还是应该将其传递给我的处理程序(如果是,该怎么做?)。</ p>

我是Golang的新手。 请帮助。</ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题