| Go 支持通过 闭包来使用 匿名函数。匿名函数在你想定义一个不需要命名的内联函数时是很实用的。 |  | 
        
        
          |  |   | 
        
        
          |  |  | 
        
        
          | 这个 intSeq函数返回另一个在intSeq函数体内定义的匿名函数。这个返回的函数使用闭包的方式 隐藏 变量i。 | func intSeq() func() int {
    i := 0
    return func() int {
        i += 1
        return i
    }
}
 | 
        
        
          |  |  | 
        
        
          | 我们调用 intSeq函数,将返回值(也是一个函数)赋给nextInt。这个函数的值包含了自己的值i,这样在每次调用nextInt时都会更新i的值。 |  | 
        
        
          | 通过多次调用 nextInt来看看闭包的效果。 |     fmt.Println(nextInt())
    fmt.Println(nextInt())
    fmt.Println(nextInt())
 | 
        
        
          | 为了确认这个状态对于这个特定的函数是唯一的,我们重新创建并测试一下。 |     newInts := intSeq()
    fmt.Println(newInts())
}
 |