Advertisement
Guest User

Untitled

a guest
May 21st, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.77 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "html/template"
  5.     "io"
  6.     "net/http"
  7.     "os"
  8. )
  9.  
  10. var tpl *template.Template
  11.  
  12. func init() {
  13.     tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
  14. }
  15.  
  16. func main() {
  17.     http.HandleFunc("/", index)
  18.     http.HandleFunc("/chair.jpeg", chair)
  19.     http.Handle("/favicon.ico", http.NotFoundHandler())
  20.     http.ListenAndServe(":8080", nil)
  21. }
  22.  
  23. func index(res http.ResponseWriter, req *http.Request) {
  24.     tpl.ExecuteTemplate(res, "index.gohtml", nil)
  25. }
  26.  
  27. func chair(res http.ResponseWriter, req *http.Request) {
  28.     f, err := os.Open("chair.jpeg")
  29.     if err != nil {
  30.         http.Error(res, "Image not found", 404)
  31.         return
  32.     }
  33.     defer f.Close()
  34.     io.Copy(res, f)
  35. }
  36. =======================================
  37. <html>
  38. <body>
  39.     <a href="/chair.jpeg">pic</a>
  40. </body>
  41. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement