How to properly use .Call in reflect package

前端 未结 2 1788
一向
一向 2021-01-02 17:48

Been having one last issue with my code which involves the .Call function in the reflect package.

So I\'m making a call such as this:

params := \"so         


        
2条回答
  •  無奈伤痛
    2021-01-02 18:34

    From the Value.Call documentation:

    Call calls the function v with the input arguments in. For example, if len(in) == 3, v.Call(in) represents the Go call v(in[0], in[1], in[2]).

    So if you want to call a function with one parameter, in must contain one reflect.Value of the right type, in your case map[string][]string.

    The expression

    in := make([]reflect.Value,0)
    

    creates a slice with length 0. Passing this to Value.Call will result in the panic you receive as you need 1 parameter, not zero.

    The correct call would be:

    m := map[string][]string{"foo": []string{"bar"}}
    
    in := []reflect.Value{reflect.ValueOf(m)}
    
    myMethod.Call(in)
    

提交回复
热议问题