krovn

Untitled

Oct 28th, 2019
127
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.     "encoding/json"
  5.     "log"
  6.     "net/http"
  7. )
  8.  
  9. type person struct {
  10.     Fname string
  11.     Lname string
  12.     Items []string
  13. }
  14.  
  15. func main() {
  16.     http.HandleFunc("/", mshl)
  17.     http.HandleFunc("/encd", encd)
  18.     http.ListenAndServe(":8080", nil)
  19. }
  20.  
  21. func mshl(w http.ResponseWriter, req *http.Request) {
  22.     w.Header().Set("Content-Type", "application/json")
  23.     p1 := person{"James", "Bond", []string{"Suit", "Gun", "Humor"}}
  24.     json, err := json.Marshal(p1)
  25.     if err != nil {
  26.         log.Fatalln(err)
  27.     }
  28.     w.Write(json)
  29. }
  30.  
  31. func encd(w http.ResponseWriter, req *http.Request) {
  32.     w.Header().Set("Content-Type", "application/json")
  33.     p1 := person{"James", "Bond", []string{"Suit", "Gun", "Humor"}}
  34.     err := json.NewEncoder(w).Encode(p1)
  35.     if err != nil {
  36.         log.Fatalln(err)
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment