如何修改for循环中的切片?

mat*_*uer 2 go

我的阅读清单中有一篇文章.每篇文章都有属性"FeedURL",其中包含文章来源的网址.当我取消订阅Feed时,我希望能够删除包含该Feed的每篇文章.

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)
}
Run Code Online (Sandbox Code Playgroud)

解决这个问题的有效方法是什么?

起初我的代码看起来像这样取消订阅:

func unsubscribe(articleList []Article, url string) []Article {
    for _, article := range articleList {
        if article.FeedURL == url {
            articleList = append(articleList[:i], articleList[i+1:]...)
        }
    }
    return articleList
}
Run Code Online (Sandbox Code Playgroud)

但后来我意识到这会改变切片并使for循环变得不可预测.

什么是一种有效而漂亮的方法来实现这一目标?

pet*_*rSO 5

要有效率:

  • 使用一段指向文章的指针,然后我们将指针移动到结构而不是结构值.
  • 如果列表中的文章顺序不重要,请使用无序算法; 它减少了指针的移动.否则,请使用有序算法.在任何情况下,最小化指针移动.
  • 不要将悬空指针留在列表的末尾.垃圾收集器会认为它们仍在使用中; 它查看切片容量而不是切片长度.
  • 最小化内存分配.

例如,

package main

import "fmt"

type Article struct {
    FeedURL string
    URL     string // should be unique
    // ... more data
}

// Remove every Article from an articleList that contains url without preserving order.
func unsubscribeUnordered(a []*Article, url string) []*Article {
    for i := 0; i < len(a); i++ {
        if a[i].FeedURL == url {
            a[len(a)-1], a[i], a = nil, a[len(a)-1], a[:len(a)-1]
            i--
        }
    }
    return a
}

// Remove every Article from an articleList that contains url while preserving order.
func unsubscribeOrdered(a []*Article, url string) []*Article {
    j := 0
    for i := 0; i < len(a); i++ {
        if a[i].FeedURL == url {
            continue
        }
        if i != j {
            a[j] = a[i]
        }
        j++
    }
    for k := j; k < len(a); k++ {
        a[k] = nil
    }
    return a[:j]
}

func NewArticleList() []*Article {
    return []*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
    }
}

func PrintArticleList(a []*Article) {
    fmt.Print("[")
    for _, e := range a {
        fmt.Printf("%+v", *e)
    }
    fmt.Println("]")
}

func main() {
    PrintArticleList(NewArticleList())
    ao := unsubscribeOrdered(NewArticleList(), "http://planet.python.org/rss20.xml")
    PrintArticleList(ao)
    auo := unsubscribeUnordered(NewArticleList(), "http://planet.python.org/rss20.xml")
    PrintArticleList(auo)
}
Run Code Online (Sandbox Code Playgroud)

输出:

[{FeedURL:http://blog.golang.org/feed.atom URL:http://blog.golang.org/race-detector}{FeedURL:http://planet.python.org/rss20.xml URL:http://archlinux.me/dusty/2013/06/29/creating-an-application-in-kivy-part-3/}{FeedURL:http://planet.python.org/rss20.xml URL:http://feedproxy.google.com/~r/cubicweborg/~3/BncbP-ap0n0/2957378}]

[{FeedURL:http://blog.golang.org/feed.atom URL:http://blog.golang.org/race-detector}]
[{FeedURL:http://blog.golang.org/feed.atom URL:http://blog.golang.org/race-detector}]
Run Code Online (Sandbox Code Playgroud)