Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.81 KB | None | 0 0
  1. // Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
  2. // License: https://creativecommons.org/licenses/by-nc-sa/4.0/
  3.  
  4. // See page 20.
  5. //!+
  6.  
  7. // Server2 is a minimal "echo" and counter server.
  8. package main
  9.  
  10. import (
  11.     "fmt"
  12.     "log"
  13.     "net/http"
  14.     "sync"
  15. )
  16.  
  17. var mu sync.Mutex
  18. var count int
  19.  
  20. func main() {
  21.     http.HandleFunc("/", handler)
  22.     http.HandleFunc("/count", counter)
  23.     log.Fatal(http.ListenAndServe("localhost:8000", nil))
  24. }
  25.  
  26. // handler echoes the Path component of the requested URL.
  27. func handler(w http.ResponseWriter, r *http.Request) {
  28.     mu.Lock()
  29.     count++
  30.     mu.Unlock()
  31.     fmt.Fprintf(w, "URL.Path = %q\n", r.URL.Path)
  32. }
  33.  
  34. // counter echoes the number of calls so far.
  35. func counter(w http.ResponseWriter, r *http.Request) {
  36.     mu.Lock()
  37.     fmt.Fprintf(w, "Count %d\n", count)
  38.     mu.Unlock()
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement