Skip to content

Instantly share code, notes, and snippets.

@kitz99
Created March 22, 2019 07:18
Show Gist options
  • Save kitz99/59ee6e3dfd8c803883439143123ac120 to your computer and use it in GitHub Desktop.
Save kitz99/59ee6e3dfd8c803883439143123ac120 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"reflect"
)
func wrapFunction(inputFunc interface{}) interface{} {
funcType, funcValue := reflect.TypeOf(inputFunc), reflect.ValueOf(inputFunc)
wrapper := reflect.MakeFunc(funcType, func(input []reflect.Value) []reflect.Value {
fmt.Println("<<<<<Start wrapping input function>>>>>")
functionResult := funcValue.Call(input)
fmt.Println("<<<<<Wrapp ended>>>>>")
return functionResult
})
return wrapper.Interface()
}
func greet(value string) string {
fmt.Println("Inside the greet function")
return "Hello, " + value
}
func helloWorld() {
fmt.Println("Hello, World!")
}
func main() {
wrappedGreet := wrapFunction(greet).(func(string) string)
fmt.Println("Value of wrappedGreet(): ", wrappedGreet("Jane Doe"))
fmt.Println("-----------------------------------")
wrappedHello := wrapFunction(helloWorld).(func())
wrappedHello()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment