Skip to content

Instantly share code, notes, and snippets.

@affix
Created January 22, 2020 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save affix/6728d7dc7862ab90a3fe6c24dd28e96f to your computer and use it in GitHub Desktop.
Save affix/6728d7dc7862ab90a3fe6c24dd28e96f to your computer and use it in GitHub Desktop.
XML to JSON in Go the full code
package function
import (
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"net/http"
)
type PostData struct {
Channel struct {
Posts []struct {
Title string `xml:"title"`
Link string `xml:"link"`
Category []string `xml:"category"`
Creator string `xml:"creator"`
PubDate string `xml:"pubDate"`
Updated string `xml:"updated"`
License string `xml:"license"`
Encoded string `xml:"encoded"`
Description string `xml:"description"`
} `xml:"item"`
} `xml:"channel"`
}
func Handle(req []byte) string {
resp, err := http.Get("https://medium.com/feed/@" + string(req))
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
data := &PostData{}
err = xml.Unmarshal(body, data)
json, _ := json.Marshal(data.Channel.Posts)
return fmt.Sprintf("%s", string(json))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment