📌  相关文章
📜  rss news google - Go 编程语言 - Go 编程语言(1)

📅  最后修改于: 2023-12-03 15:19:51.545000             🧑  作者: Mango

RSS News with Google - Go Programming Language

Introduction

In this article, we will discuss a Go programming language project for retrieving RSS news feed from Google News.

Overview

In this project, we will fetch RSS news feed from the Google News RSS feed URL using Go programming language. Then we will parse the received XML data to extract the required information from the feed.

Requirements

To work with this project, you must have the following requirements fulfilled:

  • Go version 1.15 or higher must be installed on your computer
  • Basic knowledge of Go programming language
  • An active internet connection
Implementation

First, let us install the required third-party package to work with XML data using Go programming language. Use the following command to install:

go get "encoding/xml"

Next, create a new Go file and paste the following code:

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

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

type Channel struct {
    Items []Item `xml:"item"`
}

type Rss struct {
    Channel Channel `xml:"channel"`
}

func main() {
    rssUrl := "https://news.google.com/rss?hl=en-US&gl=US&ceid=US%3Aen" //Google News RSS feed url
    resp, err := http.Get(rssUrl)
    if err != nil {
        fmt.Println("Error fetching RSS feed.", err)
        return
    }
    defer resp.Body.Close()
    
    data, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("Error reading RSS feed body.", err)
        return
    }
    
    var rss Rss
    xml.Unmarshal(data, &rss)
    for _, item := range rss.Channel.Items {
        fmt.Println(item.Title)
        fmt.Println(item.Description)
        fmt.Println(item.PubDate)
        fmt.Println(item.Link)
        fmt.Println("====================")
    }
}

This code uses the encoding/xml package to parse the received XML data. We create a struct Item that will store the title, description, publication date, and link of each news item. Similarly, we have a Channel struct that will contain all the items in the Google News RSS feed. Lastly, we have a Rss struct that will store the RSS feed XML data.

We then declare the Google News RSS feed URL and send an HTTP GET request to retrieve the feed. We use the ioutil.ReadAll function to read the HTTP response body into a byte array. We then unmarshal the byte array to the Rss struct using the xml.Unmarshal function. We loop through the items in the rss.Channel.Items slice and display the title, description, publication date, and link of each news item.

Conclusion

In this project, we have learned how to retrieve RSS news feed from Google News using Go programming language. We have also learned how to parse the received XML data using the encoding/xml package in Go programming language.