Skip to content

Instantly share code, notes, and snippets.

@shicky
Created July 15, 2016 06:48
Show Gist options
  • Save shicky/041aa613a0754d0e355c53c4989e1bb9 to your computer and use it in GitHub Desktop.
Save shicky/041aa613a0754d0e355c53c4989e1bb9 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"reflect"
)
func main() {
// register method
tasks := make(map[string]interface{})
tasks["add"] = add
// retrieve and run
task := tasks["add"]
f := reflect.ValueOf(task)
// number of arguments supported by func
numArgs := f.Type().NumIn()
log.Printf("Num Args: %d\n", numArgs)
// construct arguments
params := []interface{}{44545, 853905}
log.Printf("Num Param: %d\n", params)
// convert arguments to reflect
in := make([]reflect.Value, len(params))
for k, param := range params {
in[k] = reflect.ValueOf(param)
}
res := f.Call(in)
log.Println(res[0])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment