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返回两个值:下标值,数组实际值;

 

相关文章:

  • 2021-11-29
  • 2021-05-19
  • 2021-11-21
  • 2021-06-19
  • 2021-10-16
  • 2022-12-23
  • 2021-06-06
  • 2021-12-18
猜你喜欢
  • 2021-05-20
  • 2021-05-05
  • 2021-08-08
  • 2021-11-01
  • 2022-02-19
  • 2021-12-25
  • 2021-07-31
相关资源
相似解决方案