package feed import ( "encoding/gob" "fmt" "os" "time" "github.com/gorilla/feeds" ) type Feed struct { feeds.Feed } func NewFeed(title, link, desc string) *Feed { return &Feed{ Feed: feeds.Feed{ Title: title, Link: &feeds.Link{Href: link}, Description: desc, }, } } func SaveFeed(feed *Feed, filename string) error { file, err := os.Create(filename) if err != nil { return fmt.Errorf("error creating file %v: %v", filename, err) } defer file.Close() encoder := gob.NewEncoder(file) err = encoder.Encode(feed) if err != nil { return fmt.Errorf("error encoding file %v: %v", filename, err) } return nil } func OpenFeed(filename string) (*Feed, error) { file, err := os.Open(filename) if err != nil { return nil, fmt.Errorf("error opening file %v: %v", filename, err) } defer file.Close() feed := &Feed{} decoder := gob.NewDecoder(file) err = decoder.Decode(feed) if err != nil { return nil, fmt.Errorf("error decoding file %v: %v", filename, err) } return feed, nil } func AddToFeed(feed *Feed, title, desc, content string) error { item := feeds.Item{ Title: title, Created: time.Now(), Description: desc, Content: content, } feed.Add(&item) rss, err := feed.ToRss() if err != nil { return fmt.Errorf("error converting feed to RSS: %v", err) } fmt.Println(rss) return nil }