package main
import (
"fmt"
"log"
"net/http"
"time"
)
func writeCookie(w http.ResponseWriter, r *http.Request) {
expiration := time.Now()
expiration = expiration.AddDate(0, 0, 3)
cookie := http.Cookie{Name: "username", Value: "wufeng", Expires: expiration}
http.SetCookie(w, &cookie)
fmt.Fprintf(w, "write cookie success")
}
func readCookie(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "<html>")
cookie, _ := r.Cookie("username")
fmt.Fprint(w, cookie.Value)
fmt.Fprint(w, "<br />")
fmt.Fprint(w, "</html>")
}
func main() {
http.HandleFunc("/writeCookie", writeCookie)
http.HandleFunc("/readCookie", readCookie)
fmt.Println("服务器已启动")
err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatal(err)
}
}