Format and remove 06.1.md spaces

This commit is contained in:
vCaesar
2017-06-10 11:55:34 +08:00
parent 6cdadd518e
commit 90992c7fe1

View File

@@ -37,52 +37,52 @@ cookie是有时间限制的根据生命期不同分成两种会话cookie
Go语言中通过net/http包中的SetCookie来设置
```Go
http.SetCookie(w ResponseWriter, cookie *Cookie)
http.SetCookie(w ResponseWriter, cookie *Cookie)
```
w表示需要写入的responsecookie是一个struct让我们来看一下cookie对象是怎么样的
```Go
type Cookie struct {
Name string
Value string
Path string
Domain string
Expires time.Time
RawExpires string
type Cookie struct {
Name string
Value string
Path string
Domain string
Expires time.Time
RawExpires string
// MaxAge=0 means no 'Max-Age' attribute specified.
// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
// MaxAge>0 means Max-Age attribute present and given in seconds
MaxAge int
Secure bool
HttpOnly bool
Raw string
Unparsed []string // Raw text of unparsed attribute-value pairs
}
// MaxAge=0 means no 'Max-Age' attribute specified.
// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
// MaxAge>0 means Max-Age attribute present and given in seconds
MaxAge int
Secure bool
HttpOnly bool
Raw string
Unparsed []string // Raw text of unparsed attribute-value pairs
}
```
我们来看一个例子如何设置cookie
```Go
expiration := time.Now()
expiration = expiration.AddDate(1, 0, 0)
cookie := http.Cookie{Name: "username", Value: "astaxie", Expires: expiration}
http.SetCookie(w, &cookie)
expiration := time.Now()
expiration = expiration.AddDate(1, 0, 0)
cookie := http.Cookie{Name: "username", Value: "astaxie", Expires: expiration}
http.SetCookie(w, &cookie)
```
  
### Go读取cookie
上面的例子演示了如何设置cookie数据我们这里来演示一下如何读取cookie
```Go
cookie, _ := r.Cookie("username")
fmt.Fprint(w, cookie)
cookie, _ := r.Cookie("username")
fmt.Fprint(w, cookie)
```
还有另外一种读取方式
```Go
for _, cookie := range r.Cookies() {
fmt.Fprint(w, cookie.Name)
}
for _, cookie := range r.Cookies() {
fmt.Fprint(w, cookie.Name)
}
```
可以看到通过request获取cookie非常方便。