Go语言很好很强大,但我有几个问题想吐槽

mb5fdb0a1b25659 ·
``` func main(){ a := A([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) b := A([]int{2, 5, 8}) t.Log(a.Filter(func(i int) bool { return i >= 4 })) t.Log(a.Filter(func(i int) bool { return b.Contains(i) })) } type A []int func (a A) Filter(f func(int) bool) A { rsl := []int{} for _, v := range a { if f(v) { rsl = append(rsl, v) } } return rsl } func (a A) Contains(t int) bool { c := false for _, v := range a { if v == t { c = true break } } return c } ```
#2
更多评论
试试 type A []string func(a A)Filter(f func(string))A{ }
#1
这就是需要泛型的原因
#3