发布于 2016-01-25 09:23:43 | 125 次阅读 | 评论: 0 | 来源: 网友投递
			Go语言
Go是一种新的语言,一种并发的、带垃圾回收的、快速编译的语言。Go是谷歌2009年发布的第二款编程语言。2009年7月份,谷歌曾发布了Simple语言,它是用来开发Android应用的一种BASIC语言。		
函数作为值
Go编程语言提供灵活性,以动态创建函数,并使用它们的值。在下面的例子中,我们已经与初始化函数定义的变量。此函数变量的目仅仅是为使用内置的Math.sqrt()函数。下面是一个例子:
package mainimport (
   "fmt"
   "math"
)
func main(){
   /* declare a function variable */
   getSquareRoot := func(x float64) float64 {
      return math.Sqrt(x)
   }
   /* use the function */
   fmt.Println(getSquareRoot(9))
}
3
函数闭包
Go编程语言支持匿名函数其可以作为函数闭包。当我们要定义一个函数内联不传递任何名称,它可以使用匿名函数。在我们的例子中,我们创建了一个函数getSequence()将返回另一个函数。该函数的目的是关闭了上层函数的变量i 形成一个闭合。下面是一个例子:
package mainimport "fmt"
func getSequence() func() int {
   i:=0
   return func() int {
      i+=1
   return i  
   }
}
func main(){
   /* nextNumber is now a function with i as 0 */
   nextNumber := getSequence() 
   /* invoke nextNumber to increase i by 1 and return the same */
   fmt.Println(nextNumber())
   fmt.Println(nextNumber())
   fmt.Println(nextNumber())
   
   /* create a new sequence and see the result, i is 0 again*/
   nextNumber1 := getSequence()  
   fmt.Println(nextNumber1())
   fmt.Println(nextNumber1())
}
1
2
3
1
2