geektutu/7days-golang · error

rpc server: can't find method

Error message

rpc server: can't find method 

What it means

The service was found, but the method name has no registered methodType in svc.method. gee-rpc only publishes methods matching its criteria (exactly two args, second a pointer, one error return, all exported). This error means the requested method either does not exist or did not pass registration filtering.

Source

Thrown at gee-rpc/day6-load-balance/server.go:128

	return &h, nil
}

func (server *Server) findService(serviceMethod string) (svc *service, mtype *methodType, err error) {
	dot := strings.LastIndex(serviceMethod, ".")
	if dot < 0 {
		err = errors.New("rpc server: service/method request ill-formed: " + serviceMethod)
		return
	}
	serviceName, methodName := serviceMethod[:dot], serviceMethod[dot+1:]
	svci, ok := server.serviceMap.Load(serviceName)
	if !ok {
		err = errors.New("rpc server: can't find service " + serviceName)
		return
	}
	svc = svci.(*service)
	mtype = svc.method[methodName]
	if mtype == nil {
		err = errors.New("rpc server: can't find method " + methodName)
	}
	return
}

func (server *Server) readRequest(cc codec.Codec) (*request, error) {
	h, err := server.readRequestHeader(cc)
	if err != nil {
		return nil, err
	}
	req := &request{h: h}
	req.svc, req.mtype, err = server.findService(h.ServiceMethod)
	if err != nil {
		return req, err
	}
	req.argv = req.mtype.newArgv()
	req.replyv = req.mtype.newReplyv()

	// make sure that argvi is a pointer, ReadBody need a pointer as parameter

View on GitHub (pinned to cf36443821)

Solutions

  1. Correct the method name in the client call to match the exported Go method exactly (case-sensitive).
  2. Make the server method conform: func (f *Foo) Method(arg Req, reply *Resp) error.
  3. Ensure the method is exported (capitalized) and the receiver is a pointer type registered via Register.

Example fix

// before
func (f *Foo) Sum(a Args) Result { ... }
// after
func (f *Foo) Sum(a Args, reply *Result) error { ... }
Defensive patterns

Strategy: validation

Validate before calling

// ensure signature matches what gee-rpc publishes
var _ interface{ Sum(Args, *Result) error } = (*Foo)(nil)
if !strings.HasPrefix("Sum", strings.ToUpper("Sum")[:1]) { /* method must be exported */ }

Try / catch

if err := client.Call(ctx, "Foo.Sum", args, reply); err != nil && strings.Contains(err.Error(), "can't find method") { return fmt.Errorf("check method name/signature: %w", err) }

Prevention

When it happens

Trigger: Client requests "Foo.Sum" but Foo has no exported method Sum, or Sum has an incompatible signature (wrong arg count, no pointer receiver arg, missing error return), or the method is unexported (lowercase).

Common situations: Typo in the method name, refactoring/renaming the server-side method without updating clients, calling an unexported method, or a signature change (e.g. adding a ctx or returning a value) that silently removed the method from the published set.

Related errors


AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03). Data as JSON: /api/errors/a361daff0ccc826d. Report an issue: GitHub.