webhello2.go 662 B

123456789101112131415161718192021222324
  1. // webhello2.go
  2. package main
  3. import (
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. )
  8. func helloHandler(w http.ResponseWriter, r *http.Request) {
  9. remPartOfURL := r.URL.Path[len("/hello/"):] //get everything after the /hello/ part of the URL
  10. fmt.Fprintf(w, "Hello %s!", remPartOfURL)
  11. }
  12. func shouthelloHandler(w http.ResponseWriter, r *http.Request) {
  13. remPartOfURL := r.URL.Path[len("/shouthello/"):] //get everything after the /shouthello/ part of the URL
  14. fmt.Fprintf(w, "Hello %s!", strings.ToUpper(remPartOfURL))
  15. }
  16. func main() {
  17. http.HandleFunc("/hello/", helloHandler)
  18. http.HandleFunc("/shouthello/", shouthelloHandler)
  19. http.ListenAndServe("localhost:9999", nil)
  20. }