Skip to content

Instantly share code, notes, and snippets.

@thinhdanggroup
Created April 2, 2020 10:07
Show Gist options
  • Save thinhdanggroup/264a144d4a96631aec9ed2752b450ed5 to your computer and use it in GitHub Desktop.
Save thinhdanggroup/264a144d4a96631aec9ed2752b450ed5 to your computer and use it in GitHub Desktop.
Validate Handler and Input Params
func validateFunc(handler interface{}, nArgs int) (interface{}, error) {
method := reflect.ValueOf(handler)
f := reflect.Indirect(method)
// check type func
if f.Kind() != reflect.Func {
return f, fmt.Errorf("%T must be a Function ", f)
}
methodType := method.Type()
numIn := methodType.NumIn()
// check number of args
if nArgs < numIn {
return nil, errors.New("Call with too few input arguments")
} else if nArgs > numIn {
return nil, errors.New("Call with too many input arguments")
}
return f, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment