匿名函数

package main

import "fmt"

func main() {

	//匿名函数 1
	//f1 为函数地址
	f1 := func(x, y int) (z int) {
		z = x + y
		return
	}
	fmt.Println(f1)
	fmt.Println(f1(5, 6))

	//匿名函数 2
	//直接创建匿名函数并运行
	f2 := func(x, y int) (z int) {
		z = x + y
		return
	}(7, 8)
	fmt.Println(f2)

	//匿名函数 2 (无参数的形式)
	func() {
		fmt.Println(9 + 10)
	}() //刚开始学的时候一直不明白为什么后面要加个括号

}
0x488ba0
11
15
19

 


 

posted @ 2017-08-25 11:47  伟洪winnie  阅读(82)  评论(0编辑  收藏  举报