Skip to content

Instantly share code, notes, and snippets.

@abserari
Created August 17, 2019 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abserari/fc4f46bd0087a6688a540d2e47950c0e to your computer and use it in GitHub Desktop.
Save abserari/fc4f46bd0087a6688a540d2e47950c0e to your computer and use it in GitHub Desktop.
reflect call method grpc method
// CallUnary
// [3]reflect.Value in[0] is context.Context, in[1] is your message type which generated by pb file.
// in[2] is grpc.CallOption
func (c *Client) CallUnary(methodName string,in []reflect.Value)(interface{},error) {
m, ok := reflect.TypeOf(c.c).MethodByName(methodName)
if !ok{
log.Fatal("method not found")
return nil,errors.New("method not found")
}
if len(in) != 3{
return nil, errors.New("mistake arguement")
}
out:= m.Func.Call(in)
if len(out) != 2{
return nil, errors.New("mistake method")
}
return out[0].Interface(),out[1].Interface().(error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment