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

3 years ago
package main
import "fmt"
3 years ago
// 闭包:函数 + 函数内不能访问到变量 [能够读取其他函数内部变量的函数]
// 闭包目的: 间接访问一个变量
3 years ago
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())
}