package main import "fmt" func main() { for i := 0; i < 3; i++ { fmt.Println(outerFunc()()) } fmt.Println("------------------------") next := outerFunc() for i := 0; i < 3; i++ { fmt.Println(next()) } } func outerFunc() func() int { count := 0 return func() int { count++ fmt.Printf("**%d**\n", count) return count } }
zzh@ZZHPC:/zdata/MyPrograms/Go/testing$ go run main.go **1** 1 **1** 1 **1** 1 ------------------------ **1** 1 **2** 2 **3** 3
标签:closure,count,fmt,Println,func,Go,main,outerFunc From: https://www.cnblogs.com/zhangzhihui/p/17737647.html