Go has closures


Function literals are indeed closures.
func adder() (func(int) int) {
    var x int
    return func(delta int) int {
        x += delta
        return x
    }
f := adder()
fmt.Print(f(1))
fmt.Print(f(20))
fmt.Print(f(300))
Prints 1 21 321 - accumulating in f's x.

Comments

Popular posts from this blog

Tuning ext4 for performance with emphasis on SSD usage

Java 8: Default Interface Implementations, The Diamond Problem, Multiple Inheritance, and Precedence

Tuning ext3 for performance without losing its data integrity potential