Ai
1 Star 0 Fork 0

田圃森/learn-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 476 Bytes
一键复制 编辑 原始数据 按行查看 历史
普森 提交于 2023-03-28 20:58 +08:00 . go基础
package main
import "fmt"
type filterFunc func(n int) bool
func isEven(n int) bool { return n%2 == 0 }
func isOdd(m int) bool { return m%2 == 1 }
func filter(f filterFunc, nums ...int) (filtered []int) {
for _, n := range nums {
if f(n) {
filtered = append(filtered, n)
}
}
return
}
func main() {
nums := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Printf("evens : %d\n", filter(isEven, nums...))
fmt.Printf("odds : %d\n", filter(isOdd, nums...))
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/pustian/learn-go.git
git@gitee.com:pustian/learn-go.git
pustian
learn-go
learn-go
a7405e71a3e9

搜索帮助