inline.go 529 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "text/template"
  6. )
  7. type tplParams struct {
  8. URL string
  9. Browser string
  10. }
  11. const EXAMPLE = `
  12. Browser {{.Browser}}
  13. you at {{.URL}}
  14. `
  15. func handle(w http.ResponseWriter, r *http.Request) {
  16. tmpl := template.New(`example`)
  17. tmpl, _ = tmpl.Parse(EXAMPLE)
  18. params := tplParams{
  19. URL: r.URL.String(),
  20. Browser: r.UserAgent(),
  21. }
  22. tmpl.Execute(w, params)
  23. }
  24. func main() {
  25. http.HandleFunc("/", handle)
  26. fmt.Println("starting server at :8080")
  27. http.ListenAndServe(":8080", nil)
  28. }