http://blog.51cto.com/speakingbaicai/1703229https://www.jianshu.com/p/fa21e6fada70
所谓闭包就是一个函数“捕获”了和它在同一作用于的其他常量和变量。
package main import "fmt" func addNumber(x int) func(int) { fmt.Printf("x: %d, addr of x:%p\n", x, &x) return func(y int) { k := x + y x = k y = k fmt.Printf("x: %d, addr of x:%p\n", x, &x) fmt.Printf("y: %d, addr of y:%p\n", y, &y) } } func main() { addNum := addNumber(5) addNum(1) addNum(1) addNum(1) fmt.Println("---------------------") addNum1 := addNumber(5) addNum1(1) addNum1(1) addNum1(1) } x: 5, addr of x:0xc4200140c8 x: 6, addr of x:0xc4200140c8 y: 6, addr of y:0xc420014100 x: 7, addr of x:0xc4200140c8 y: 7, addr of y:0xc420014118 x: 8, addr of x:0xc4200140c8 y: 8, addr of y:0xc420014130 --------------------- x: 5, addr of x:0xc420014148 x: 6, addr of x:0xc420014148 y: 6, addr of y:0xc420014158 x: 7, addr of x:0xc420014148 y: 7, addr of y:0xc420014170 x: 8, addr of x:0xc420014148 y: 8, addr of y:0xc420014188 posted on 2018-09-13 11:43 北京涛子 阅读( ...) 评论( ...) 编辑 收藏转载于:https://www.cnblogs.com/liujitao79/p/9639650.html