Created
March 22, 2019 07:18
-
-
Save kitz99/59ee6e3dfd8c803883439143123ac120 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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