Skip to content

Instantly share code, notes, and snippets.

@kwmt
Created October 9, 2013 02:49
Show Gist options
  • Select an option

  • Save kwmt/6895428 to your computer and use it in GitHub Desktop.

Select an option

Save kwmt/6895428 to your computer and use it in GitHub Desktop.
リフレクションを使って関数をコールする。
package main
import (
"fmt"
"reflect"
)
func f(n uint, m int) uint{
return n+ uint(m)
}
func main() {
fv := reflect.ValueOf(f)
arg1 := reflect.ValueOf(uint(100)) //reflect.Value
arg2 := reflect.ValueOf(200) //reflect.Value
//関数コール
result := fv.Call([]reflect.Value{arg1, arg2}) //[]reflect.Value ←戻り値も複数返せる為、スライスとなっている
fmt.Println(result[0].Uint())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment