filter

下面是一个示例代码:

package main

import "fmt"

type Item struct {
    id   int
    name string
}

func main() {
    items := []*Item{
        {1, "apple"},
        {2, "banana"},
        {3, "orange"},
    }

    idToDelete := 2
    items = filter(items, func(item *Item) bool {
        return item.id != idToDelete
    })

    fmt.Printf("%+v", items)
}

func filter(items []*Item, predicate func(*Item) bool) []*Item {
    var result []*Item
    for _, item := range items {
        if predicate(item) {
            result = append(result, item)
        }
    }
    return result
}
Itemfilter
itemsid=2items
itemsid=2

运行结果如下:

[{id:1 name:apple} {id:3 name:orange}]