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
389 B

package main
import "fmt"
// 闭包:能够读取其他函数内部变量的函数
// 通过闭包方式隐藏了变量i
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())
}