英文:
Delete first item in slice if length > 100
问题
当RSS订阅更新时(目前没有更新,只是虚拟数据),新的项目会被追加到"feed"切片中。随着时间的推移,这可能意味着它包含数百万个项目,我不希望出现这种情况。
因此,当切片中的项目超过100个时,应该从顶部(第0个项目)开始删除项目。在这个例子中,我使用一个只有100个项目的RSS文件,所以下面的示例代码应该在50个项目之后从顶部开始删除:
package main
import (
"fmt"
"github.com/SlyMarbo/rss"
"time"
)
var feed *rss.Feed
var directory = "./dump"
func main() {
for {
checkRSS()
// 每分钟检查一次feed.Refresh是否已经过去,以便运行update()
time.Sleep(1 * time.Minute)
}
}
func checkRSS() (*rss.Feed, error) {
var err error
// 如果feed仍为空,则首先获取它,以便我们可以运行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\n", length)
fmt.Printf("Refreshing at %s\n", feed.Refresh)
return feed, err
}
英文:
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\n", length)
fmt.Printf("Refreshing at %s\n", feed.Refresh)
return feed, err
}
答案1
得分: 1
如果订阅源中的项目数量超过限制,可以对其进行切片:
length := len(feed.Items)
if length > limit {
feed.Items = feed.Items[length - limit:]
}
当长度超过限制时,新的长度将恰好为限制值。
在这里不需要使用for循环。
英文:
If the number of items in the feed grows over the limit, slice it:
length := len(feed.Items)
if length > limit {
feed.Items = feed.Items[length - limit:]
}
When the length is over the limit, the new length will be exactly limit.
You don't need a for loop there.
答案2
得分: 0
为了实现这个,你可能想要使用切片操作。假设你想要从feed
中移除x
个项目,你可以简单地使用feed = feed[x:]
,它将返回索引x-1
之后的所有项目,并将其重新赋值给feed
切片。如果在你的实际代码中你只想要移除第一个项目,那么可以使用feed = feed[1:]
。
英文:
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:]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论