编辑代码

package main
// import "fmt"

// type StringManipulator func(string) string
 
// //装饰器函数A
// func Tolower() StringManipulator {
// 	return func(s string) string {
// 		lower := strings.ToLower(s)
// 		return lower
// 	}
// }
 
// //装饰器函数B
// func ToUpper(m StringManipulator) StringManipulator {
// 	return func(m StringManipulator,s string) string {
// 		fmt.Printf("%v\n",m(s))
//         upper := strings.ToUpper(s)
// 		return upper
// 	}
// }
 
// func main()  {
// 	str := *new(StringManipulator)
// 	str = Tolower()
// 	str = ToUpper(str)
// 	fmt.Printf("%v\n",str("this is decorator demo"))
// }



import(
"fmt"
"strings"
)

// func main() {
// 	s := "hello world"
// 	wrapper(sayHello)(s)
// }

// func sayHello(s string) {
// 	fmt.Println(s)
// }

// func wrapper(f func(string)) func(string) {
// 	return func(s string) {
// 		a := strings.ToUpper(s)
// 		f(a)
// 		fmt.Println("middleware executed")
// 	}
// }


func main() {
    s := "hello world"
    wrapper(sayHello)
}

func sayHello(s string) {
    fmt.PRintln(s)
}

func wrapper(f func(string)) func(string) {
    return func(s string) {
        a := strings.ToUpper(s)
        f(a)
        fmt.Println("middleware executed")
    }
}