server2.go 490 B

123456789101112131415161718192021222324252627282930
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "sync"
  7. )
  8. var mu sync.Mutex
  9. var count int
  10. func main() {
  11. http.HandleFunc("/", handler_)
  12. http.HandleFunc("/count", counter)
  13. log.Fatal(http.ListenAndServe("localhost:8000", nil))
  14. }
  15. func handler_(w http.ResponseWriter, r *http.Request) {
  16. mu.Lock()
  17. count++
  18. mu.Unlock()
  19. fmt.Fprintf(w, "URL.Path = %q\n", r.URL.Path)
  20. }
  21. func counter(w http.ResponseWriter, r *http.Request) {
  22. mu.Lock()
  23. fmt.Fprintf(w, "Count %d\n", count)
  24. mu.Unlock()
  25. }