Advertisement
Guest User

Untitled

a guest
May 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.07 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "html/template"
  5.     "io/ioutil"
  6.     "net/http"
  7. )
  8.  
  9. var tpl *template.Template
  10.  
  11. func init() {
  12.     tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
  13. }
  14.  
  15. func main() {
  16.     http.HandleFunc("/", index)
  17.     http.Handle("/favicon.ico", http.NotFoundHandler())
  18.     http.ListenAndServe(":8080", nil)
  19. }
  20.  
  21. func index(res http.ResponseWriter, req *http.Request) {
  22.     var s string
  23.     if req.Method == http.MethodPost {
  24.         f, _, err := req.FormFile("fails")
  25.         if err != nil {
  26.             http.Error(res, err.Error(), http.StatusInternalServerError)
  27.             return
  28.         }
  29.         defer f.Close()
  30.         bs, err := ioutil.ReadAll(f)
  31.         if err != nil {
  32.             http.Error(res, err.Error(), http.StatusInternalServerError)
  33.             return
  34.         }
  35.         s = string(bs)
  36.     }
  37.     tpl.ExecuteTemplate(res, "index.gohtml", s)
  38. }
  39.  
  40. =============================================
  41.  
  42. <!DOCTYPE html>
  43. <html>
  44. <head>
  45. <title>GoLang :P</title>
  46. </head>
  47. <body>
  48.     <form method="Post" enctype="multipart/form-data">
  49.         <input type="file" name="fails">
  50.         <input type="submit">
  51.         <br><br>
  52.         <h3>{{.}}</h3>
  53.     </form>
  54. </body>
  55. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement