Skip to content

Instantly share code, notes, and snippets.

@robertscherbarth
Created July 20, 2018 08:58
Show Gist options
  • Save robertscherbarth/bc166dd8616f106b3e5f5f5528c66147 to your computer and use it in GitHub Desktop.
Save robertscherbarth/bc166dd8616f106b3e5f5f5528c66147 to your computer and use it in GitHub Desktop.
Wrapper function for Go
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