Created
January 22, 2020 21:11
-
-
Save affix/6728d7dc7862ab90a3fe6c24dd28e96f to your computer and use it in GitHub Desktop.
XML to JSON in Go the full code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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