路由管理器 ServeMux
func main() {
myMux := http.NewServeMux()
myMux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
_, _ = writer.Write([]byte("hello"))
})
_ = http.ListenAndServe(":8089", myMux)
}
func main() {
myMux := http.NewServeMux()
myMux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
_, _ = writer.Write([]byte("hello"))
})
myMux.HandleFunc("/login", func(writer http.ResponseWriter, request *http.Request) {
_, _ = writer.Write([]byte("这是登录页面"))
})
_ = http.ListenAndServe(":8089", myMux)
}
cookie
myMux := http.NewServeMux()
myMux.HandleFunc("/login", func(writer http.ResponseWriter, request *http.Request) {
userName := request.URL.Query().Get("user_name")
if userName != "" {
cookie := &http.Cookie{Name:"userName",Value:userName,Path:"/"}
http.SetCookie(writer,cookie)
}
_, _ = writer.Write([]byte("这是登录页面"))
})
_ = http.ListenAndServe(":8089", myMux)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c20de00b4e2c0693d5cdf09541f22a11.png)
清除cookie
myMux.HandleFunc("/logout", func(responseWriter http.ResponseWriter, request *http.Request) {
cookie := &http.Cookie{Name: "userName", Value: "", Path: "/", Expires: time.Now().AddDate(-1, 0, 0)}
http.SetCookie(responseWriter, cookie)
})