Go语言学习笔记(5)

博客介绍了cookie相关知识,如通过脚本获取、设置httponly防止js获取、设置path和存活时间等。还提及Restful,包括Go语言默认路由表,使用第三方github库gorilla/mux实现请求路由和调度,满足特定条件的请求可被识别。

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}条件的可以被识别

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值