今天项目中要将map存入cookie,于是大象哥研究了下json和base64,写了个小例子
一、将map存入cookie
//要存入cookie的map your := map[string]string{} your["isuser"] = isuser your["username"] = username your["password"] = d.Strtomd5(password) //将map转成json 转换后的是[]byte,需要string(your_byte)后就是json了 your_byte,_ := json.Marshal(your) //将json base64一下 b64 := base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") your_string := b64.EncodeToString(your_byte) fmt.Println(your_string) //第二种base64的方式 your_string = base64.StdEncoding.EncodeToString(your_byte) fmt.Println(your_string) //存储cookie this.Ctx.SetCookie("your",your_string)
//读取cookie中的变量 your_string := this.Ctx.Input.Cookie("your") //解析成json b64 := base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") your_to_byte,_ := b64.DecodeString(your_string) your := string(your_to_byte) fmt.println(your)原文链接:https://www.f2er.com/go/190932.html