Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "net/http"
- "strconv"
- "io"
- "fmt"
- )
- func main() {
- http.HandleFunc("/", index)
- http.Handle("/favicon.ico", http.NotFoundHandler())
- http.ListenAndServe(":8080", nil)
- }
- func index(w http.ResponseWriter, r *http.Request) {
- count, err := r.Cookie("count")
- var currentCount int
- if err != nil {
- // Cookie doesn't exist
- currentCount = 1
- http.SetCookie(w, &http.Cookie{
- Name: "count",
- Value: strconv.Itoa(currentCount),
- })
- } else {
- currentCount, err = strconv.Atoi(count.Value)
- // Not convertable to string
- if err != nil {
- currentCount = 1
- } else {
- currentCount++
- }
- count.Value = strconv.Itoa(currentCount)
- }
- io.WriteString(w, fmt.Sprintf("Your current visit count is %d", currentCount))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement