package main import . "fmt" //notice 1 type testInt func(uint32) bool func isOdd(integer uint32) bool {
if integer% == {
return false
}
return true
} func isEven(integer uint32) bool {
if integer% == {
return true
}
return false
} func filter(slice []uint32, f testInt) []uint32 {
var result []uint32
for _,value := range slice { //notice 2
if f(value) {
result = append(result, value)
}
}
return result
} func main() {
slice := []uint32 {,,,,,}
Println( slice) odd := filter(slice, isOdd)
Println("Odd elements of slice are : ", odd) even := filter(slice, isEven)
Println("Even elements of slice are : ", even)
}
注意点一,引入包的方法;
注意点二,range返回两个值:下标值,数组实际值;