Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to modify a slice within for loop?
    text
    copied!<p>I've got a slice of articles in my reading list. Each Article has the attribute "FeedURL" that has the URL of the feed the article came from. When I unsubscribe from a feed, I want to be able to remove every Article that contains that Feed's URL.</p> <pre><code>type Article struct { FeedURL string URL string // should be unique // ... more data } func unsubscribe(articleList []Article, url string) []Article { // how do I remove every Article from articleList that contains url? } func main() { myArticleList := []Article{ Article{"http://blog.golang.org/feed.atom", "http://blog.golang.org/race-detector"}, Article{"http://planet.python.org/rss20.xml", "http://archlinux.me/dusty/2013/06/29/creating-an-application-in-kivy-part-3/"}, Article{"http://planet.python.org/rss20.xml", "http://feedproxy.google.com/~r/cubicweborg/~3/BncbP-ap0n0/2957378"}, // ... much more examples } myArticleList = unsubscribe(myArticleList, "http://planet.python.org/rss20.xml") fmt.Printf("%+v", myArticleList) } </code></pre> <p>What is the efficient way of solving this problem?</p> <p>At first my code looked like this for unsubscribe:</p> <pre><code>func unsubscribe(articleList []Article, url string) []Article { for _, article := range articleList { if article.FeedURL == url { articleList = append(articleList[:i], articleList[i+1:]...) } } return articleList } </code></pre> <p>But then I realized that this would change the slice and make the for loop unpredictable.</p> <p>What is an efficient and pretty way to accomplish this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload