func redirectHandler(w http.ResponseWriter, r *http.Request) {
	np := r.URL.Path[1:]
	n, err := strconv.Atoi(np)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if n > 0 {
		u := r.URL
		u.Path = fmt.Sprintf("/%d", n-1)
		http.Redirect(w, r, u.String(), http.StatusMovedPermanently)
		return
	}
	http.Error(w, "Hello World!", http.StatusOK)
}
redirectHandler called with a path of /<n> will redirect to /<n-1> if n>0.
A path of /0 prints "Hello World" and any other path results in a 500 error.
package main
import (
	"flag"
	"fmt"
	"log"
	"math/rand"
	"net/http"
	"strconv"
	"time"
)
var (
	port = flag.String("port", ":4490", "Port on localhost to run the demo server.")
)
func main() {
	flag.Parse()
	http.HandleFunc("/", homeHandler)
	http.HandleFunc("/echo", echoHandler)
	http.HandleFunc("/redirect", redirectHandler)
	http.HandleFunc("/cookie", cookieHandler)
	log.Fatal(http.ListenAndServe(*port, nil))
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, `<!doctype html>
<html>
<head><title>Demo Server</title></head>
<body>
<h1>Welcome to the demo server</h1>
</bod>
</html>`)
}