Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "encoding/json"
- "log"
- "net/http"
- )
- type person struct {
- Fname string
- Lname string
- Items []string
- }
- func main() {
- http.HandleFunc("/", mshl)
- http.HandleFunc("/encd", encd)
- http.ListenAndServe(":8080", nil)
- }
- func mshl(w http.ResponseWriter, req *http.Request) {
- w.Header().Set("Content-Type", "application/json")
- p1 := person{"James", "Bond", []string{"Suit", "Gun", "Humor"}}
- json, err := json.Marshal(p1)
- if err != nil {
- log.Fatalln(err)
- }
- w.Write(json)
- }
- func encd(w http.ResponseWriter, req *http.Request) {
- w.Header().Set("Content-Type", "application/json")
- p1 := person{"James", "Bond", []string{"Suit", "Gun", "Humor"}}
- err := json.NewEncoder(w).Encode(p1)
- if err != nil {
- log.Fatalln(err)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment