12345678910111213141516171819202122232425262728293031323334 |
- // Server2 is a minimal "echo" and counter server.
- package main
- import (
- "fmt"
- "log"
- "net/http"
- "sync"
- )
- var mu sync.Mutex
- var count int
- func main() {
- http.HandleFunc("/", handler)
- http.HandleFunc("/count", counter)
- log.Fatal(http.ListenAndServe("localhost:8000", nil))
- }
- // handler echoes the Path component of the requested URL.
- func handler(w http.ResponseWriter, r *http.Request) {
- mu.Lock()
- count++
- mu.Unlock()
- fmt.Fprintf(w, "URL.Path = %q\n", r.URL.Path)
- }
- // counter echoes the number of calls so far.
- func counter(w http.ResponseWriter, r *http.Request) {
- mu.Lock()
- fmt.Println(r)
- fmt.Fprintf(w, "Count %d\n", count)
- mu.Unlock()
- }
|