56

关于go中beego/session的小总结

 4 years ago
source link: https://studygolang.com/articles/20401?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

1.用数据库作为存储介质的时候 语法是怎样的?参照 https://github.com/go-sql-driver/mysql#dsn-data-source-name

下面是我在使用的连接方式

nmMJji6.png!web

(还有一个报错的地方是因为我只引入了session包,没有引入session/mysql),这些引入细节在session/mysql里面都有介绍,所以看文档的时候要注意读这些部分

JNbYVjn.png!web

还有一点是必须手动添加session表,这个在session_mysql.go里面也有说明

2.在init()函数配置的写法

比如 sess_mysql.go里面的 配置参数的写法是一个字符串,但是实际copy起来,编辑器里会报错,要换成一个结构体

yyA7Fre.png!web

如下

把配置从字符串改写成 session.ManagerConfig结构体类型作为 session.NewManager()的第二个参数

eEzqUjf.png!web

3.浏览器的cookie没有保存session_id

我这里用了 https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/06.2.md 里面的login函数,发现浏览器的cookie并没有把当前的session_id保存下来

最后发现是原始文档的配置值简单的配置了 cookieName, gclifetime ,providerConfig,其它的参数没有配置,比如

EnableSetCookie这个参数 ,必须设置为true才会像http写入cookie的值

下面附上一个用beego/session的简单例子

IF7FzyA.png!web
QFJ3um7.png!web

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK