# Golang 闭包实现装饰器 **Published by:** [Robin](https://paragraph.com/@robin-8/) **Published on:** 2023-02-24 **URL:** https://paragraph.com/@robin-8/golang-3 ## Content type MultiPlyFunc func(int, int) int func multiply1(a, b int) int { return a * b } func multiply2(a, b int) int { return a << b } func exctTime(f MultiPlyFunc) MultiPlyFunc { return func(a, b int) int { start := time.Now() c := f(a, b) end := time.Since(start) fmt.Printf("---执行耗时:%v ---\n", end) return c } } func main() { a := 2 b := 8 fmt.Println("算术运算:") decorator := exctTime(multiply1) c := decorator(a, b) fmt.Printf("%d x %d = %d\n", a, b, c) fmt.Println("位运算: ") decorator2 := exctTime(multiply2) a = 1 b = 4 c = decorator2(1, b) fmt.Printf("%d << %d = %d\n", a, b, c) } ## Publication Information - [Robin](https://paragraph.com/@robin-8/): Publication homepage - [All Posts](https://paragraph.com/@robin-8/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@robin-8): Subscribe to updates