Created
July 20, 2018 08:58
-
-
Save robertscherbarth/bc166dd8616f106b3e5f5f5528c66147 to your computer and use it in GitHub Desktop.
Wrapper function for Go
This file contains 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" | |
) | |
// This is an example of an Function that takes a function an various parameters. | |
func callMe(fn interface{}, params ...interface{}) (result []reflect.Value) { | |
f := reflect.ValueOf(fn) | |
if f.Type().NumIn() != len(params) { | |
panic("Wrong number of params") | |
} | |
inputs := make([]reflect.Value, len(params)) | |
for key, val := range params { | |
inputs[key] = reflect.ValueOf(val) | |
} | |
return f.Call(inputs) | |
} | |
func printString(number int){ | |
fmt.Printf("The Number is: %v", number ) | |
} | |
func main() { | |
callMe(printString, 2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment