awesome-patterns/structural/decorator/decorate.go

30 lines
462 B
Go
Raw Permalink Normal View History

2019-10-30 22:31:07 +03:00
package main
2020-05-08 12:07:20 +03:00
import "log"
2019-10-30 22:31:07 +03:00
type Object func(int) int
func LogDecorate(fn Object) Object {
return func(n int) int {
log.Println("Starting the execution with the integer", n)
result := fn(n)
log.Println("Execution is completed with the result", result)
2020-05-08 12:07:20 +03:00
return result
2019-10-30 22:31:07 +03:00
}
}
func Double(n int) int {
2020-05-08 12:07:20 +03:00
return n * 2
2019-10-30 22:31:07 +03:00
}
2020-05-08 12:07:20 +03:00
func main() {
f := LogDecorate(Double)
f(5)
2019-10-30 22:31:07 +03:00
}
// Starting execution with the integer 5
// Execution is completed with the result 10