表紙 編集 差分 一覧 最近 短縮 付箋 ログイン 凍結中

Accumulator Generator

N2Wiki > Go言語学習帳 > 04_AccumulatorGenerator

AccumulatorGenerator

package main
import "fmt"

func main() {
    f := accgen(10);
    a := f(1);
    b := f(2);
    fmt.Printf("a: %d, b: %d \n",a,b);
}

func accgen(n int) (func(int) int) {
    return func(x int) int {
        n += x;
        return n;
    };
}
  • あとで気づいたけどtestのclosure.goの中にあった。
  • return n += x; と書くと叱られます。