Skip to content

Commit 72d959b

Browse files
authored
Merge pull request astaxie#1157 from 0uep/patch-1
Add SameSite in Cookie struct
2 parents 89cc47f + 85f6881 commit 72d959b

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

en/06.1.md

+26-25
Original file line numberDiff line numberDiff line change
@@ -40,44 +40,45 @@ Go uses the `SetCookie` function in the `net/http` package to set cookies:
4040
```
4141
`w` is the response of the request and cookie is a struct. Let's see what it looks like:
4242
```Go
43-
type Cookie struct {
44-
Name string
45-
Value string
46-
Path string
47-
Domain string
48-
Expires time.Time
49-
RawExpires string
50-
51-
// MaxAge=0 means no 'Max-Age' attribute specified.
52-
// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
53-
// MaxAge>0 means Max-Age attribute present and given in seconds
54-
MaxAge int
55-
Secure bool
56-
HttpOnly bool
57-
Raw string
58-
Unparsed []string // Raw text of unparsed attribute-value pairs
59-
}
43+
type Cookie struct {
44+
Name string
45+
Value string
46+
Path string // optional
47+
Domain string // optional
48+
Expires time.Time // optional
49+
RawExpires string // for reading cookies only
50+
51+
// MaxAge=0 means no 'Max-Age' attribute specified.
52+
// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
53+
// MaxAge>0 means Max-Age attribute present and given in seconds
54+
MaxAge int
55+
Secure bool
56+
HttpOnly bool
57+
SameSite SameSite
58+
Raw string
59+
Unparsed []string // Raw text of unparsed attribute-value pairs
60+
}
6061
```
6162
Here is an example of setting a cookie:
6263

6364
```Go
64-
expiration := time.Now().Add(365 * 24 * time.Hour)
65-
cookie := http.Cookie{Name: "username", Value: "astaxie", Expires: expiration}
66-
http.SetCookie(w, &cookie)
65+
expiration := time.Now().Add(365 * 24 * time.Hour)
66+
cookie := http.Cookie{Name: "username", Value: "astaxie", Expires: expiration}
67+
http.SetCookie(w, &cookie)
6768
```
6869

6970
## Fetch cookies in Go
7071

7172
The above example shows how to set a cookie. Now let's see how to get a cookie that has been set:
7273
```Go
73-
cookie, _ := r.Cookie("username")
74-
fmt.Fprint(w, cookie)
74+
cookie, _ := r.Cookie("username")
75+
fmt.Fprint(w, cookie)
7576
```
7677
Here is another way to get a cookie:
7778
```Go
78-
for _, cookie := range r.Cookies() {
79-
fmt.Fprint(w, cookie.Name)
80-
}
79+
for _, cookie := range r.Cookies() {
80+
fmt.Fprint(w, cookie.Name)
81+
}
8182
```
8283
As you can see, it's very convenient to get cookies from requests.
8384

0 commit comments

Comments
 (0)