Menu Close

Go – Register Function in net/rpc package in Go

In this article, we will explore the Register Function in net/rpc package in Go in detail, along with examples.


Go, also known as Golang, is a versatile programming language designed for creating concurrent and networked applications. One of the standard libraries provided by Go is the ‘net/rpc’ package, which helps developers create and use Remote Procedure Calls (RPC) for communication between various application components. In this article, we will focus on the Register function, an essential feature of the ‘net/rpc’ package, and discuss its purpose, usage, and a practical example to better understand its functionality.

What is Register Function ?

The Register function is part of the ‘net/rpc’ package in Go, which primarily deals with creating RPC clients and servers. This function is used to register a service object, which represents the methods that the server can call remotely. The methods of the service object must satisfy specific rules to be exposed to the RPC system.

Function Signature

The function signature for Register is as follows:

func Register(rcvr interface{}) error

The Register function takes an ‘interface{}’ type as input, which represents the service object to be registered. If the registration is successful, the function returns nil; otherwise, it returns an error containing information about the issue.

Rules for Service Objects

To be registered and exposed as an RPC service, the methods of the service object must satisfy the following rules:

  1. The method must be exported (have an uppercase first letter).
  2. The method must have two arguments, both of which are pointers to exported types.
  3. The method must return an error.


Let’s take a look at an example to understand how the Register function works in conjunction with the ‘net/rpc’ package:

package main

import (

type Greeter struct{}

func (g *Greeter) Greet(name *string, reply *string) error {

	*reply = fmt.Sprintf("Hello, %s!", *name)

	return nil

func main() {

	addr := "localhost:12345"

	greeter := new(Greeter)

	err := rpc.Register(greeter)

	if err != nil {
		fmt.Println("Error registering Greeter:", err)

	listener, err := net.Listen("tcp", addr)

	if err != nil {
		fmt.Println("Error listening:", err)
	defer listener.Close()

	fmt.Printf("RPC server listening on %s\n", addr)


In this example, we create a simple RPC server that responds to Greet function calls. We define a ‘Greeter’ type and a ‘Greet’ method for that type. The ‘Greet’ method takes a name as input and returns a greeting message.

We then create an instance of ‘Greeter’ and register it with the ‘net/rpc’ package using ‘rpc.Register()’. After successful registration, we create a listener using ‘net.Listen()’ on ‘localhost:12345’ and call the ‘Accept()’ function on the ‘rpc’ default server instance to serve incoming RPC requests.

Note that this example demonstrates the server-side implementation of the RPC system. To fully test the functionality, you would need to create a separate client-side implementation to connect to this server and call the ‘Greet’ method.


The Register function in Go’s ‘net/rpc’ package is a key component for building RPC servers. By registering service objects and their methods with the RPC system, it allows seamless communication between different application components. The example above demonstrates the simplicity of using the Register function along with the ‘net/rpc’ package to create an RPC server.

To check more Go related articles. Pls click given below link:

Posted in golang, net, packages

Leave a Reply

Your email address will not be published. Required fields are marked *