Golang

 

Introduction

An RSS feed reader allows users to retrieve and display data from RSS feeds. With Go programming language, we can build a simple RSS feed reader that fetches and parses feed data from the internet. This tutorial will guide you through creating such a program from scratch, providing the necessary tools to retrieve and display the latest headlines from a given RSS feed URL.

Objective

The objective of this program is to show how to write an RSS feed reader using the Go programming language. The program will fetch an RSS feed, parse it, and display the titles of the latest items from the feed. By the end of this tutorial, you will have a working RSS reader application and an understanding of how to work with XML data in Go.

RSS Feed Reader Code in Go


package main

import (
	"encoding/xml"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
)

// Define the structure for parsing the RSS feed
type RSS struct {
	Channel Channel `xml:"channel"`
}

type Channel struct {
	Title       string `xml:"title"`
	Description string `xml:"description"`
	Link        string `xml:"link"`
	Items       []Item `xml:"item"`
}

type Item struct {
	Title   string `xml:"title"`
	Link    string `xml:"link"`
	PubDate string `xml:"pubDate"`
}

func main() {
	// RSS feed URL
	url := "https://example.com/rss_feed.xml"

	// Send an HTTP GET request to fetch the RSS feed
	response, err := http.Get(url)
	if err != nil {
		log.Fatalf("Error fetching RSS feed: %v", err)
	}
	defer response.Body.Close()

	// Read the body of the response
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		log.Fatalf("Error reading response body: %v", err)
	}

	// Parse the RSS feed
	var rss RSS
	err = xml.Unmarshal(body, &rss)
	if err != nil {
		log.Fatalf("Error parsing XML: %v", err)
	}

	// Display the title and the first 5 items
	fmt.Println("Feed Title:", rss.Channel.Title)
	for i, item := range rss.Channel.Items {
		if i >= 5 {
			break
		}
		fmt.Printf("%d. %s\n", i+1, item.Title)
		fmt.Println("   Link:", item.Link)
		fmt.Println("   Published Date:", item.PubDate)
	}
}
        

Explanation of the Program Structure

Let’s break down the program structure:

  • Imports: The program imports several packages: encoding/xml to parse the XML format of the RSS feed, fmt for formatting and printing output, io/ioutil to read data from the HTTP response, log for error logging, and net/http for making the HTTP request.
  • RSS Structs: The RSS struct represents the RSS feed. Inside it, there is a Channel struct, which holds the details of the feed such as title, description, and items. Each item in the feed is represented by the Item struct, which contains the title, link, and publication date.
  • HTTP Request: The program uses http.Get to make an HTTP request to the given RSS feed URL, retrieving the XML data.
  • Parsing XML: The XML data is then unmarshalled (parsed) into Go structs using the xml.Unmarshal function.
  • Displaying the Feed: The program prints out the title of the feed and the titles of the first 5 items, along with their links and publication dates.

How to Run the Program

  1. Make sure you have Go installed on your system. You can download and install Go from here.
  2. Copy the Go code provided above and save it in a file, for example rss_reader.go.
  3. Replace the url variable in the code with the URL of an actual RSS feed you want to parse.
  4. Open your terminal or command prompt and navigate to the directory where the rss_reader.go file is saved.
  5. Run the Go program by typing the following command:
    go run rss_reader.go
  6. The program will fetch the RSS feed, parse it, and display the feed title and the first 5 item titles along with their links and publication dates.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)