我正在阅读本课https://gobyexample.com/collection-functions并看到下一个代码:
package main
import (
"fmt"
"strings"
)
func Any(vs []string, f func(string) bool) bool {
for _, v := range vs {
if f(v) {
return true
}
}
return false
}
func main() {
var strs = []string{"peach", "apple", "pear", "plum"}
fmt.Println(Any(strs, func(v string) bool {
return strings.HasPrefix(v, "p")
}))
}
使用匿名函数它工作正常。但是如何在那里使用命名函数呢?我试过了:
...
fmt.Println(Any(strs, strings.HasPrefix("p")))
但是得到:
./prog.go:96:41: not enough arguments in call to strings.HasPrefix
have (string)
want (string, string)
./prog.go:96:41: cannot use strings.HasPrefix("p") (type bool) as type func(string) bool in argument to Any
我究竟做错了什么?
12345678_0001
相关分类