Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.58 KB | None | 0 0
  1. // Package p contains an HTTP Cloud Function.
  2. package p
  3.  
  4. import (
  5.     "encoding/json"
  6.     "fmt"
  7.     "html"
  8.     "net/http"
  9. )
  10.  
  11. // HelloWorld prints the JSON encoded "message" field in the body
  12. // of the request or "Hello, World!" if there isn't one.
  13. func HelloWorld(w http.ResponseWriter, r *http.Request) {
  14.     var d struct {
  15.         Message string `json:"message"`
  16.     }
  17.     if err := json.NewDecoder(r.Body).Decode(&d); err != nil {
  18.         fmt.Fprint(w, "Hello World!")
  19.         return
  20.     }
  21.     if d.Message == "" {
  22.         fmt.Fprint(w, "Hello World!")
  23.         return
  24.     }
  25.     fmt.Fprint(w, html.EscapeString(d.Message))
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement