项目地址: https://github.com/ahuigo/gofnext#decorator-cases
类似于 python 的 @functools.cache
cache decorator 菲波那切数列示例:
package main
import "fmt"
import "github.com/ahuigo/gofnext"
func main() {
var fib func(int) int
var fibCached func(int) int
fib = func(x int) int {
fmt.Printf("call arg:%d\n", x)
if x <= 1 {
return x
} else {
return fibCached(x-1) + fibCached(x-2)
}
}
fibCached = gofnext.CacheFn1(fib)
fmt.Println(fibCached(5))
fmt.Println(fibCached(6))
}