cookie
package main
import (
"html/template"
"net/http"
)
func welcome(w http.ResponseWriter, r *http.Request){
t,_:=template.ParseFiles("view/index.html")
t.Execute(w,nil)
}
func setCookie(w http.ResponseWriter, r *http.Request){
c:=http.Cookie{Name:"mykey",Value:"myvalue"}
http.SetCookie(w,&c)
t,_:=template.ParseFiles("view/index.html")
t.Execute(w,nil)
}
func main(){
server:=http.Server{Addr:":8090"}
http.HandleFunc("/",welcome)
server.ListenAndServe()
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>cookie</title>
<script type="text/javascript" src="/static/js/jquery-3.4.1.min.js"></script>
</head>
<body>
<a href="setCookie">产生Cookie</a>
</body>
</html>
package main
import (
"html/template"
"net/http"
)
func welcome(w http.ResponseWriter, r *http.Request){
t,_:=template.ParseFiles("view/index.html")
t.Execute(w,nil)
}
func setCookie(w http.ResponseWriter, r *http.Request){
c:=http.Cookie{Name:"mykey",Value:"myvalue"}
http.SetCookie(w,&c)
t,_:=template.ParseFiles("view/index.html")
t.Execute(w,nil)
}
func getCookie(w http.ResponseWriter, r *http.Request){
//根据key取出cookie
//c1,_:=r.Cookie("mykey")
//取出全部cookie
cs:=r.Cookies()
t,_:=template.ParseFiles("view/index.html")
t.Execute(w,cs)
}
func main(){
server:=http.Server{Addr:":8090"}
http.HandleFunc("/",welcome)
http.HandleFunc("/setCookie",setCookie)
http.HandleFunc("/getCookie",getCookie)
server.ListenAndServe()
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>cookie</title>
<script type="text/javascript" src="/static/js/jquery-3.4.1.min.js"></script>
</head>
<body>
<a href="setCookie">set Cookie</a>
<br/>
<a href="getCookie">get Cookie</a>
<br/>
{{.}}
</body>
</html>
通过脚本获取cookie
package main
import (
"html/template"
"net/http"
)
func welcome(w http.ResponseWriter, r *http.Request){
t,_:=template.ParseFiles("view/index.html")
t.Execute(w,nil)
}
func doCookie(w http.ResponseWriter, r *http.Request){
c:=http.Cookie{Name:"mykey",Value:"myvalue"}
http.SetCookie(w,&c)
t,_:=template.ParseFiles("view/index.html")
t.Execute(w,nil)
}
func main(){
server:=http.Server{Addr:":8090"}
http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir("static"))))
http.HandleFunc("/",welcome)
http.HandleFunc("/doCookie",doCookie)
server.ListenAndServe()
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>cookie</title>
<script type="text/javascript" src="/static/js/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="/static/js/jquery.cookie.js"></script>
<script type="text/javascript">
$(function() {
$("button").click(function (){
var $value=$.cookie("mykey")
alert($value)
})
})
</script>
</head>
<body>
<a href="doCookie">产生cookie</a>
<br/>
<button>获取Cookie</button>
<br/>
{{.}}
</body>
</html>
通过设置httponly=true可以防止js获得cookie
func doCookie(w http.ResponseWriter, r *http.Request){
c:=http.Cookie{Name:"mykey",Value:"myvalue",HttpOnly:true}
http.SetCookie(w,&c)
t,_:=template.ParseFiles("view/index.html")
t.Execute(w,nil)
}
path路径设置cookie 可访问范围
c:=http.Cookie{Name:"mykey",Value:"myvalue",Path:"/abc/"}
Expires 存活时间
Maxage 类似
// 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
c:=http.Cookie{Name:"mykey",Value:"myvalue",Expires:time.Date(2019,05,05,22,00,00,0,time.Local)}
增加时间
time.Now().Add()
Restful
go语言默认的路由表
type ServeMux struct {
mu sync.RWMutex
m map[string]muxEntry //存放具体的路由信息
}
使用第三方github库
go get github.com/gorilla/mux
Package gorilla/mux implements a request router and dispatcher for matching incoming requests to their respective handler.
The name mux stands for “HTTP request multiplexer”. Like the standard http.ServeMux, mux.Router matches incoming requests against a list of registered routes and calls a handler for the route that matches the URL or other conditions.
package main
import (
"fmt"
"github.com/gorilla/mux"
"net/http"
)
func demo(w http.ResponseWriter, r *http.Request){
vars:=mux.Vars(r)
fmt.Fprintln(w,vars["url"])
}
func main(){
r:=mux.NewRouter()
r.HandleFunc("/cz/{url}",demo)
http.ListenAndServe(":8090",r)
}
满足/cz/{url}条件的可以被识别