GO语言(一)G语言自虐

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

 

posted on 2013-09-02 13:33  笔记吧... 可能只有自己看得懂  阅读(1062)  评论(0编辑  收藏  举报