在浏览器中设置Cookies:Golang

huangapple go评论73阅读模式
英文:

Setting up Cookies at browser : Golang

问题

作为一个在Golang上的新手,我试图在浏览器上设置cookies,我有一个简单的基本代码,但它根本不起作用,我做了一些搜索并找到了一些stackoverflow的例子,但仍然没有找到正确的方法。

创建了一个简单的hello.go文件:

package main

import "io"
import "net/http"
import "time"

func handler(w http.ResponseWriter, req *http.Request) {
    expire := time.Now().AddDate(0, 0, 1)
    cookie := http.Cookie{"test", "tcookie", "/", "www.dummy.com", expire, expire.Format(time.UnixDate), 86400, true, true, "test=tcookie", []string{"test=tcookie"}}
    req.AddCookie(&cookie)
    io.WriteString(w, "Hello world!")
}

func main() {
    http.HandleFunc("/", handler)
}

但是如预期的那样,我遇到了错误,如\hello.go:9:15: composite struct literal net/http.Cookie with untagged fields

请问有人可以给我建议或者给我一个详细的设置cookies的基本示例吗?

我在stackoverflow上进行了一些搜索并找到了以下链接:https://stackoverflow.com/questions/12130582/setting-cookies-in-golang-net-http/12130619#12130619,但是我无法正确理解它。

谢谢。

英文:

As am newbie here on Golang, trying to setup cookies at browser,
have simple basic code but it doesn't working at all & did some googling & found some stackoverflow ex. but still not picking up the right way.

Created a simple hello.go

package main

import "io"
import "net/http"
import "time"

func handler(w http.ResponseWriter, req *http.Request) {
    expire := time.Now().AddDate(0, 0, 1)
    cookie := http.Cookie{"test", "tcookie", "/", "www.dummy.com", expire, expire.Format(time.UnixDate), 86400, true, true, "test=tcookie", []string{"test=tcookie"}}
    req.AddCookie(&cookie)
    io.WriteString(w, "Hello world!")
}

func main() {
    http.HandleFunc("/", handler)
}

But as expected here am facing error's like \hello.go:9:15: composite struct literal net/http.Cookie with untagged fields

Could any one please suggest me or give me basic example (in detailing) for setting up cookies.

Had few searches on SO and found..
https://stackoverflow.com/questions/12130582/setting-cookies-in-golang-net-http/12130619#12130619 but not able to picking up this properly..

Thanks.

答案1

得分: 6

好的,以下是翻译好的部分:

在你提供的问题中,基本上是建议使用net/http中的这个函数:

func SetCookie(w ResponseWriter, cookie *Cookie)

所以在你的例子中,不要写

req.AddCookie(&cookie)

而应该写成

http.SetCookie(w, &cookie)
英文:

Well in the question you link to it basically says to use this function from net/http:

func SetCookie(w ResponseWriter, cookie *Cookie)

So in your example instead of writing

req.AddCookie(&cookie)

you should write this

http.SetCookie(w, &cookie)

答案2

得分: 6

cookie := &http.Cookie{Name:"test", Value:"tcookie", Expires:time.Now().Add(356*24*time.Hour), HttpOnly:true}

after this

change

req.AddCookie(&cookie)

to

http.SetCookie(w, cookie)

and finally because your application is running on Google App Engine change:

func main()

to

func init()

英文:

cookie := http.Cookie{"test", "tcookie", "/", "www.dummy.com", expire, expire.Format(time.UnixDate), 86400, true, true, "test=tcookie", []string{"test=tcookie"}}

should be something like this:

cookie := &http.Cookie{Name:"test", Value:"tcookie", Expires:time.Now().Add(356*24*time.Hour), HttpOnly:true}

after this

change

req.AddCookie(&cookie)

to

http.SetCookie(w, cookie)

and finally because your application is running on Google App Engine change:

func main()

to

func init()

huangapple
  • 本文由 发表于 2013年6月13日 19:34:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/17086065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定