5

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.. Setting Cookies in Golang (net/http) but not able to picking up this properly..

Thanks.

Community
  • 1
  • 1
eegloo
  • 161
  • 2
  • 9
  • @stephanos: Yeh, am aware about this, but not able to use that either, Could you please explore that in details, Would really appreciate your reply on this... – eegloo Jun 13 '13 at 11:41
  • Try http://www.gorillatoolkit.org/pkg/sessions with the default cookie back end. – elithrar Jun 13 '13 at 12:39
  • Yes Trying that.... but how to import online packages on my local machine (Window 7 64bits), In Go SDK there is gopath folder.. downloaded three .go files which are require in this session.go but not able to add local path in import. Path of gopath folder is set in the environmental variable but still is is giving error of "failed parsing dir ", "not able to import from so so path"...etc.. ..What can be done ? – eegloo Jun 13 '13 at 13:25
  • If your GOPATH is set correctly, just use `go get` as per the instructions. There is no need to download the files manually. – elithrar Jun 13 '13 at 13:32
  • what version of go are you using ? type `go version` – fabrizioM Jun 13 '13 at 17:40
  • @fabrizioM : go version go1.0.3 (appengine-1.8.0) – eegloo Jun 14 '13 at 04:37

2 Answers2

6

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)
stephanos
  • 3,319
  • 7
  • 33
  • 47
  • Thanks, Still am getting same error, like `go-app-builder: Failed parsing input (1 error)` and `composite struct literal net/http.Cookie with untagged fields` – eegloo Jun 13 '13 at 11:45
  • Am i missing some thing else here? Any guesses or idea about these error – eegloo Jun 13 '13 at 11:49
  • Tried Full test program: from https://code.google.com/p/go/issues/detail?id=3033 link But on browser "404 page not found" this is displaying.... – eegloo Jun 13 '13 at 12:24
6

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()

Neo
  • 149
  • 5
  • Thanks a lot. Also Replace that "func main()" to "func init()" .... Then it is Working. – eegloo Jun 14 '13 at 10:31
  • you're welcome, it should not be required to change the `func main()` to `func init()` – Neo Jun 14 '13 at 13:45
  • 1
    @Neo for Google App Engine you need to use `func init()` instead of `func main()`. A bit outside the question, but OP is on Google App Engine as you can see from the tagging of the question...just a detail. – murrekatt Mar 31 '15 at 08:52