跳到主要内容

RSS Feed

Github StackBlitz

本项目展示了如何使用 Fiber 框架在 Go 应用中创建 RSS 订阅。

前提条件

确保你已安装以下软件

设置

  1. 克隆仓库

    git clone https://github.com/gofiber/recipes.git
    cd recipes/rss-feed
  2. 安装依赖

    go get

运行应用

  1. 启动应用
    go run main.go

示例

这里是如何在 Fiber 应用中创建 RSS 订阅的示例

package main

import (
"github.com/gofiber/fiber/v2"
"github.com/gorilla/feeds"
"time"
)

func main() {
app := fiber.New()

app.Get("/rss", func(c *fiber.Ctx) error {
feed := &feeds.Feed{
Title: "Example RSS Feed",
Link: &feeds.Link{Href: "http://example.com/rss"},
Description: "This is an example RSS feed",
Author: &feeds.Author{Name: "John Doe", Email: "john@example.com"},
Created: time.Now(),
}

feed.Items = []*feeds.Item{
{
Title: "First Post",
Link: &feeds.Link{Href: "http://example.com/post/1"},
Description: "This is the first post",
Author: &feeds.Author{Name: "John Doe", Email: "john@example.com"},
Created: time.Now(),
},
{
Title: "Second Post",
Link: &feeds.Link{Href: "http://example.com/post/2"},
Description: "This is the second post",
Author: &feeds.Author{Name: "Jane Doe", Email: "jane@example.com"},
Created: time.Now(),
},
}

rss, err := feed.ToRss()
if err != nil {
return err
}

c.Set("Content-Type", "application/rss+xml")
return c.SendString(rss)
})

app.Listen(":3000")
}

参考