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.

Revisions

  1. legendtkl created this gist Aug 31, 2016.
    32 changes: 32 additions & 0 deletions funcCallbyReflect.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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)
    }