How To Create RSS Feeds In Golang

Integrate RSS feed from backend
Feb 23 2021 · 2 min read

Introduction

In the era of digitalization, we have to keep our users up to date with frequently changing things in our applications.

One of the finest solutions is to use RSS(Really Simple Syndication) feeds.

We are what we repeatedly do. Excellence, then, is not an act, but a habit. Try out Justly and start building your habits today!

What are RSS Feeds?

It is data or web feeds that allow users and applications to access regular updates from websites(from which they want updates) in a readable format.

Users have to subscribe to RSS Feeds for regular updates.

RSS Feeds are available in XML-formatted plain text.

Here is a sample XML format of the RSS feed.

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
 <title>RSS Feed's Title</title>
 <description>Description of an RSS feed</description>
 <link>http://www.rssfeedslink.com</link>
 <copyright>All rights reserved</copyright>
 <pubDate>Sat, 23 jan 2021 16:20:00 +0000</pubDate>

 <item>
  <title>First Item</title>
  <description>Description Of Item</description>
  <link>http://www.example.com/blog/post/1</link>
  <pubDate>Sat, 23 jan 2021 16:20:00 +0000</pubDate>
 </item>

</channel>
</rss>

You can find more about it here


Create RSS Feeds in Go

We will Create RSS feeds from JSON data. You can also generate RSS feeds from the database data.

We are using github.com/gorilla/feeds for generating RSS feeds.

First Create Main RSS feed data like the below,

import "github.com/gorilla/feeds"
feed := &feeds.Feed{
	    Title:       "Title of Your Feeds",
	    Link:        &feeds.Link{Href: "your feed link"},
	    Description: "Description of your feeds",
	    Author:      &feeds.Author{Name: "author name"},
	    Created:     time.Now(),
	   }

Then append items on a feed from selected database data using the following code.

var feedItems []*feeds.Item
for i := 0; i < len(feedsData); i++ {  
       item := feedsData[i]
       feedItems = append(feedItems,
              &feeds.Item{
                   Id:item.ID,
                   Title:item.Title,
                   Link:&feeds.Link{Href: "feeds item link"},
                   Description:item.Description,
                   Created:time.Now(),
                })
}
feed.Items = feedItems

Here you can append your data as per your requirements either in increasing or decreasing order.

Convert these feed items to original RSS Feeds using the RssFeed() method, and generate XML formatted feed

rssFeed := (&feeds.Rss{Feed: feed}).RssFeed()
xmlRssFeeds := rssFeed.FeedXml()

Finishing Thoughts

That’s it. We can pass these data to API as well as web applications.

You can refer to the full source code here.

We’re Grateful to have you with us on this journey!

Suggestions and feedback are more than welcome! 

Please reach us at Canopas Twitter handle @canopas_eng with your content or feedback. Your input enriches our content and fuels our motivation to create more valuable and informative articles for you.

Similar Articles


sumita-k image
Sumita Kevat
Sumita is an experienced software developer with 5+ years in web development. Proficient in front-end and back-end technologies for creating scalable and efficient web applications. Passionate about staying current with emerging technologies to deliver.


sumita-k image
Sumita Kevat
Sumita is an experienced software developer with 5+ years in web development. Proficient in front-end and back-end technologies for creating scalable and efficient web applications. Passionate about staying current with emerging technologies to deliver.

Whether you need...

  • *
    High-performing mobile apps
  • *
    Bulletproof cloud solutions
  • *
    Custom solutions for your business.
Bring us your toughest challenge and we'll show you the path to a sleek solution.
Talk To Our Experts
footer
Subscribe Here!
Follow us on
2024 Canopas Software LLP. All rights reserved.