Un script de génération de flux RSS en go
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 main | |
import ( | |
"encoding/xml" | |
"fmt" | |
"io/ioutil" | |
"path/filepath" | |
"time" | |
) | |
type rss struct { | |
Version string `xml:"version,attr"` | |
Title string `xml:"channel>title"` | |
Link string `xml:"channel>link"` | |
Description string `xml:"channel>description"` | |
LastBuildDate string `xml:"channel>lastBuildDate"` | |
Item []rssItem `xml:"channel>item"` | |
} | |
type rssItem struct { | |
Title string `xml:"title"` | |
Link string `xml:"link"` | |
Description string `xml:"description"` | |
Image string `xml:"image"` | |
} | |
func main() { | |
articles := []rssItem{ | |
{ | |
Title: "Comment passer au monorepo avec Lerna", | |
Link: "https://blog.otso.fr/2020-05-08-passez-en-monorepo-avec-lerna.html", | |
Description: "Suivez-moi pas à pas dans la découverte du monde fantastique des monorepos !", | |
Image: "https://blog.otso.fr/images/2020-05-08-passez-en-monorepo-avec-lerna/rainbow.gif", | |
}, | |
{ | |
Title: "Les 10 erreurs les plus fréquentes que j’ai rencontrées sur des projets Go", | |
Link: "https://blog.otso.fr/2019-12-26-top-10-erreurs-communes-projets-golang.html", | |
Description: "Cet article représente mon top 10 des erreurs les plus fréquemment rencontrées sur mes projets Go. L’ordre n’a pas d’importance.", | |
Image: "https://blog.otso.fr/images/2019-12-26-top-10-erreurs-communes-projets-golang/facepalm.jpg", | |
}, | |
} | |
rssStruct := &rss{ | |
Version: "2.0", | |
Title: "Blob Trotter", | |
Link: "https://blog.otso.fr", | |
Description: "Les pérégrinations d'un développeur dans le monde de Javascript et de Go", | |
LastBuildDate: time.Now().Format(time.RFC1123Z), | |
Item: articles, | |
} | |
data, err := xml.MarshalIndent(rssStruct, "", " ") | |
if err != nil { | |
fmt.Println(err) | |
} | |
rssFeed := []byte(xml.Header + string(data)) | |
if err := ioutil.WriteFile(filepath.Join("./", "rss.xml"), rssFeed, 0644); err != nil { | |
fmt.Println(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment