Mahdi.Kh
September 21, 2026
Very briefly and simply, we get familiar with the standard RPC package provided in Go and learn how to use RPC for communication between services.
RPC, or Remote Procedure Call, is one way to create communication between services. There are other methods too, for example:
Usually, when using RPC, the preference is for data to be transferred in binary form, so that less bandwidth is consumed and data transfer happens faster. There are different ways to convert data to binary; for example, you can use Protocol Buffers (protobuffer) to convert data to binary and back.
In Go, using the standard net/rpc library, we can create a server and a client to exchange data with each other. In the net/rpc package, data is converted to binary using the gob format by default. But you can also use your own preferred format to encode and decode data.
If you'd like to know more about gob, you can read about it in our previous post.
The RPC package that Go provides as a standard can be used for communication between services over HTTP or TCP.
In Go, to implement RPC, we need to consider at least three things:
Creating a suitable service for RPC
An RPC server
An RPC client
Implementing a standard method between the server and client for encoding and decoding data
To create an RPC service, we need to create a data type that has a set of methods. These methods can be run using RPC.
The data type and methods must meet the following conditions:
The data type must be accessible outside its current package (must be exported).
The methods that will be run by RPC must be accessible outside the current package (must be exported).
The methods that will be run by RPC must have two arguments. These arguments must also have exported data types.
The method's first argument represents the value or values the method accepts as input. The method's second argument must be a pointer, into which the function's return response or output is placed.
The method must return a value of type error.
For example, we create a data type named Arith and add two methods to it named Multiply and Divide:
The Multiply function performs multiplication on two numbers and returns the result.
The Devide function takes two numbers as input, divides the first number by the second, and finally returns the quotient and remainder.
In the example above, the Arith data type has two methods, and both of these methods have two arguments. The first argument of both methods is a pointer of type Args, which is itself a struct with two numeric values. So, as input, you can store two numbers in a variable of type Args and then pass that variable to the methods.
Finally, the output or return value of each method is also placed inside that method's second parameter.
In Go, using the standard net/rpc package, we can easily create an RPC server over the TCP or HTTP protocols.
To create a TCP server, we can proceed as follows:
In the example above, we created a pointer from Arith and then registered it with the RPC server using the Register method. Finally, we ran the RPC server on port 1234.
Now, by creating an RPC client, we can run commands either synchronously or asynchronously:
The Arith data type must be shared on both the server and client sides so that data exchange can be done correctly.
To create an RPC server over HTTP, we can proceed as follows:
In the example above, we created a main server and a server for debugging! When you use HTTP, a debug server is implemented by default, which you can use for the development environment. In the example above, we ran the debug server on port 1235. The debug server, at http://localhost:1235/debug/rpc, gives us some information about the available RPC methods and the number of calls made.
In the next step, by creating an RPC client, we can run commands either synchronously or asynchronously:
Note that the Arith data type, as well as the data types used in its methods' arguments, must be shared between the server and the client so that data can be correctly converted to binary and back.
The net/rpc package converts data to binary using the gob format by default, but you can also use your own preferred formats. For example, you can use protobuf (or protobuffer) or json.
The resources used to write this post:
You can get the sample code in this post from our GitHub repository.
Previous part: Converting Data to Binary Using Gob | Golang basics
Next part: Error Handling | Golang basics