Go supports anonymous functions, which can form closures. Anonymous functions are useful when you want to define a function inline without having to name it
package main import ( "fmt" ) func intSeq() func() int { i := 0 return func() int { i += 1 return i } } func main() { nextInt := intSeq() fmt.Println(nextInt()) fmt.Println(nextInt()) fmt.Println(nextInt()) nextInts := intSeq() fmt.Println(nextInts()) }
1 2 3 1
总结 :
1 : 匿名函数内部的值不是暴露在外面的....
时间: 2024-10-16 21:18:00