i trying create go cookie. want assign id mongodb stored in cookie. while compiling getting error follows:-
"unknown http.cookie field 'id' in struct literal"
the following code:-
getuser := user.checkdb() expiration := time.now().add(365 * 24 * time.hour) //the error caused next line cookie := http.cookie{id: getuser[0].id, name: getuser[0].email, value: getuser[0].password, expires: expiration} http.setcookie(w, &cookie) func (this *user) checkdb() []user { var results []user sess, db := getdatabase() defer sess.close() c := db.c("user") uname := &this.email err := c.find(bson.m{"email": *uname}).sort("-id").all(&results) if err != nil { panic(err) } else { fmt.println("results all: ", results) return results } } type cookie struct { id bson.objectid `bson:"_id,omitempty"` name string value string path string domain string expires time.time rawexpires string maxage int secure bool httponly bool raw string unparsed []string }
thanks in advance.
here solution problem.
cookie struct below:
type cookie struct { name string value string path string domain string expires time.time rawexpires string maxage int secure bool httponly bool raw string unparsed []string }
cookie creation
value := map[string]string{ "id": cookieid, } if encoded, err := ckiehandler.encode("session", value); err == nil { cookie := &http.cookie{ name: "session", value: encoded, path: "/", } http.setcookie(response, cookie) }
cookie call
if cookie, err := request.cookie("session"); err == nil { cookievalue := make(map[string]string) if err = ckiehandler.decode("session", cookie.value, &cookievalue); err == nil { id = cookievalue["id"] // **pass bson id here** } }
for more details click here. link helped me lot. hoping find answer useful.
Comments
Post a Comment