geektutu/7days-golang · error
rpc: service already defined:
Error message
rpc: service already defined:
What it means
Duplicate-registration guard in Server.Register: LoadOrStore found the service name already present in serviceMap, so publishing the same receiver type twice is rejected. It fires when Register is called twice for one service, which would otherwise silently overwrite the method table.
Source
Thrown at gee-rpc/day3-service/server.go:197
}
go server.ServeConn(conn)
}
}
// Accept accepts connections on the listener and serves requests
// for each incoming connection.
func Accept(lis net.Listener) { DefaultServer.Accept(lis) }
// Register publishes in the server the set of methods of the
// receiver value that satisfy the following conditions:
// - exported method of exported type
// - two arguments, both of exported type
// - the second argument is a pointer
// - one return value, of type error
func (server *Server) Register(rcvr interface{}) error {
s := newService(rcvr)
if _, dup := server.serviceMap.LoadOrStore(s.name, s); dup {
return errors.New("rpc: service already defined: " + s.name)
}
return nil
}
// Register publishes the receiver's methods in the DefaultServer.
func Register(rcvr interface{}) error { return DefaultServer.Register(rcvr) }
View on GitHub (pinned to cf36443821)
Solutions
- Call Register once per service; guard startup code with sync.Once if needed.
- Use a different receiver type/name if two services genuinely share a name.
- Treat the returned error as a startup misconfiguration and fail fast.
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at gee-rpc/day3-service/server.go:197 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03).
Data as JSON: /api/errors/c41febca44b922b8.
Report an issue: GitHub.