Skip to content

Instantly share code, notes, and snippets.

@legendtkl
Created August 31, 2016 05:58
Show Gist options
  • Save legendtkl/f737f75a407d9e4f1312ef02cd15919d to your computer and use it in GitHub Desktop.
Save legendtkl/f737f75a407d9e4f1312ef02cd15919d to your computer and use it in GitHub Desktop.
func call by reflect
package main
import (
"reflect"
"fmt"
)
type Test struct {
fn interface{}
args []reflect.Value
}
func foo(t Test) {
reflect.ValueOf(t.fn).Call(t.args)
}
func bar(i, j int) {
fmt.Println(i+j)
}
func main() {
f := bar
x, y := 3, 5
arg := []reflect.Value{reflect.ValueOf(x), reflect.ValueOf(y)}
test := Test{
fn: f,
args: arg,
}
foo(test)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment