func (s *service) registerMethods() {
s.method = make(map[string]*methodType)
for i := 0; i < s.typ.NumMethod(); i++ {
method := s.typ.Method(i)
mType := method.Type
if mType.NumIn() != 3 || mType.NumOut() != 1 {
continue
}
if mType.Out(0) != reflect.TypeOf((*error)(nil)).Elem() {
continue
}
argType, replyType := mType.In(1), mType.In(2)
if !isExportedOrBuiltinType(argType) || !isExportedOrBuiltinType(replyType) {
continue
}
s.method[method.Name] = &methodType{
method: method,
ArgType: argType,
ReplyType: replyType,
}
log.Printf("rpc server: register %s.%s\n", s.name, method.Name)
}
}
what does reflect.TypeOf((*error)(nil)).Elem()
mean in this code? I know if mType.Out(0) != reflect.TypeOf((*error)(nil)).Elem()
is trying to determine if the method's return type is error not not. But for me, reflect.TypeOf((error)(nil))
intuitively will do the same, but unfortunately not. When I try to compile this code, it says type error is not an expression, what does it mean in this context? Does not reflect.Typeof()
accepts a argument of certain type? I found that (*error)(nil) is equivalent to *error = nil. I am confused with this expression.
error(nil)
givesnil
,(*error)(nil)
gives*error
– Inoculate