Created
October 23, 2019 09:46
-
-
Save jayjayswal/d3de0bee5c72cf69fe02b66bb9d3cd7e to your computer and use it in GitHub Desktop.
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 ( | |
"errors" | |
"fmt" | |
"reflect" | |
) | |
//func foo() (arr string) { | |
// arr="foo called" | |
// fmt.Println("we are running foo") | |
// return arr | |
//} | |
// | |
//func bar(a, b, c int) (arr string) { | |
// arr="bar called" | |
// fmt.Println("we are running bar", a, b, c) | |
// return arr | |
//} | |
// | |
//func Call(m map[string]interface{}, name string, params ... interface{}) (result []reflect.Value, err error) { | |
// f := reflect.ValueOf(m[name]) | |
// if len(params) != f.Type().NumIn() { | |
// err = errors.New("The number of params is not adapted.") | |
// return | |
// } | |
// fmt.Println(params) | |
// //var values []interface{} | |
// in := make([]reflect.Value, len(params)) | |
// for k, param := range params { | |
// in[k] = reflect.ValueOf(param) | |
// } | |
// result = f.Call(in) | |
// return | |
//} | |
// | |
//func main() { | |
// // nota bene: for perfect score: use reflection to build this map | |
// funcs := map[string]interface{} { | |
// "foo": foo, | |
// "bar": bar, | |
// } | |
// | |
// //Call(funcs, "foo") | |
// Call(funcs, "bar", 1, 2, "a") | |
//} | |
//type My struct{} | |
// | |
//func (m My) foo() (arr string) { | |
// arr="foo called" | |
// fmt.Println("we are running foo") | |
// return arr | |
//} | |
// | |
//func (m My) bar(v ...interface{}) (arr string) { | |
// arr="bar called" | |
// | |
// fmt.Println("we are running bar") | |
// fmt.Println(v...) | |
// return arr | |
//} | |
// | |
// | |
//func main() { | |
// // nota bene: for perfect score: use reflection to build this map | |
// //funcs := map[string]interface{}{ | |
// // "foo": foo, | |
// // "bar": bar, | |
// //} | |
// | |
// m := My{} | |
// meth := reflect.ValueOf(m).MethodByName("foo") | |
// meth.Call(nil) | |
// //funcs["foo"]() | |
//} | |
type My struct{} | |
func (m My) MyFunc() { | |
fmt.Println("MyFunc called") | |
} | |
func (m My) Foo() (arr string) { | |
arr="returned value by foo" | |
fmt.Println("Foo called") | |
return arr | |
} | |
func (m My) Bar(a int,b int,c string) (arr string) { | |
arr="returned value by Bar" | |
fmt.Println("Bar called",a,b,c) | |
return arr | |
} | |
func (m My) callFunction(name string,params ...interface{}) (res []reflect.Value,err error){ | |
defer func() { | |
if r := recover(); r != nil { | |
switch r.(type) { | |
case string: | |
err = errors.New(r.(string)) | |
default: | |
err = r.(error) | |
} | |
} | |
}() | |
meth := reflect.ValueOf(m).MethodByName(name) | |
if len(params) !=meth.Type().NumIn(){ | |
err = errors.New("The number of params is not adapted.") | |
return | |
} | |
in := make([]reflect.Value, len(params)) | |
for k, param := range params { | |
in[k] = reflect.ValueOf(param) | |
} | |
res = meth.Call(in) | |
return | |
} | |
//func main() { | |
// m := My{} | |
// fmt.Println("========================") | |
// res, err:=m.callFunction("MyFunc") | |
// fmt.Println("error: ",err) | |
// fmt.Println(res) | |
// | |
// fmt.Println("========================") | |
// res, err=m.callFunction("Foo") | |
// fmt.Println("error: ",err) | |
// fmt.Println(res) | |
// | |
// | |
// fmt.Println("========================") | |
// res, err=m.callFunction("Bar",1,4,"33") | |
// fmt.Println("error: ",err) | |
// fmt.Println(res) | |
// | |
// fmt.Println("========================") | |
// fmt.Println("the show must go on") | |
// | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment