dongpangfu6322 2016-11-01 22:29
浏览 18

如果长度> 100,则删除切片中的第一项

When the RSS feeds updates (it doesn't right now, just dummy data) the new items are appended to the "feed" slice. Over time this could mean that it contains millions of items, I don't want that.

So when there are more than 100 items in the slice it should delete items starting from the top (item 0). In this example I'm using an RSS file with ust 100 items so the sample code below should delete from the top after 50 items:

package main

import (
    "fmt"
    "github.com/SlyMarbo/rss"
    "time"
)

var feed *rss.Feed
var directory = "./dump"

func main() {
    for {
        checkRSS()
        // Check every minute if feed.Refresh has passed so it run's update()
        time.Sleep(1 * time.Minute)
    }
}

func checkRSS() (*rss.Feed, error) {
    var err error
    // If feed is still empty fetch it first so we can run update()
    if feed == nil {
        feed, err = rss.Fetch("http://cloud.dgier.nl/api.xml")
    } else {
        err = feed.Update()
    }
    length := len(feed.Items)
    for key, value := range feed.Items {
        fmt.Println(key, value.Title, value.Read)
        if key >= 50 {
            fmt.Println("Item key is > 50")

        }
    }
    fmt.Printf("Current length: %d
", length)
    fmt.Printf("Refreshing at %s
", feed.Refresh)
    return feed, err
}
  • 写回答

2条回答 默认 最新

  • duanou9758 2016-11-01 22:33
    关注

    To achieve this you probably want to use subslicing. Say you want to remove x items from feed, you can simply do feed = feed[x:] which will yield all items after index x-1 and assign it back to the feed slice. If in your actual code you just want to remove the first item then it would be feed = feed[1:]

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测