You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
435 B
25 lines
435 B
package main
|
|
|
|
import "fmt"
|
|
|
|
// 闭包:函数 + 函数内不能访问到变量 [能够读取其他函数内部变量的函数]
|
|
// 闭包目的: 间接访问一个变量
|
|
func ins() func() int {
|
|
i := 0
|
|
return func() int {
|
|
i++
|
|
return i
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
nextInt := ins()
|
|
fmt.Println(nextInt())
|
|
fmt.Println(nextInt())
|
|
fmt.Println(nextInt())
|
|
|
|
// 特定的函数变量是唯一的
|
|
nextInts := ins()
|
|
fmt.Println(nextInts())
|
|
}
|