Created
August 31, 2016 05:58
-
-
Save legendtkl/f737f75a407d9e4f1312ef02cd15919d to your computer and use it in GitHub Desktop.
func call by reflect
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 ( | |
"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