在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1 package main 2 3 import . "fmt" //notice 1 4 5 type testInt func(uint32) bool 6 7 func isOdd(integer uint32) bool { 8 if integer%2 == 0 { 9 return false 10 } 11 return true 12 } 13 14 func isEven(integer uint32) bool { 15 if integer%2 == 0 { 16 return true 17 } 18 return false 19 } 20 21 func filter(slice []uint32, f testInt) []uint32 { 22 var result []uint32 23 for _,value := range slice { //notice 2 24 if f(value) { 25 result = append(result, value) 26 } 27 } 28 return result 29 } 30 31 func main() { 32 slice := []uint32 {1,2,3,4,5,7} 33 Println( slice) 34 35 odd := filter(slice, isOdd) 36 Println("Odd elements of slice are : ", odd) 37 38 even := filter(slice, isEven) 39 Println("Even elements of slice are : ", even) 40 }
注意点二,range返回两个值:下标值,数组实际值;
|
请发表评论