Help us shape the future of Go! Take the 2026 Go Developer Survey

Source file src/net/rpc/doc.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  /*
     6  Package rpc provides access to the exported methods of an object across a
     7  network or other I/O connection.
     8  
     9  The net/rpc package is frozen and is not accepting new features.
    10  
    11  A server registers an object, making it visible
    12  as a service with the name of the type of the object.  After registration, exported
    13  methods of the object will be accessible remotely.  A server may register multiple
    14  objects (services) of different types but it is an error to register multiple
    15  objects of the same type.
    16  
    17  Only methods that satisfy these criteria will be made available for remote access;
    18  other methods will be ignored:
    19  
    20    - the method's type is exported.
    21    - the method is exported.
    22    - the method has two arguments, both exported (or builtin) types.
    23    - the method's second argument is a pointer.
    24    - the method has return type error.
    25  
    26  In effect, the method must look schematically like
    27  
    28  	func (t *T) MethodName(argType T1, replyType *T2) error
    29  
    30  where T1 and T2 can be marshaled by encoding/gob.
    31  These requirements apply even if a different codec is used.
    32  (In the future, these requirements may soften for custom codecs.)
    33  
    34  The method's first argument represents the arguments provided by the caller; the
    35  second argument represents the result parameters to be returned to the caller.
    36  The method's return value, if non-nil, is passed back as a string that the client
    37  sees as if created by [errors.New].  If an error is returned, the reply parameter
    38  will not be sent back to the client.
    39  
    40  The server may handle requests on a single connection by calling [ServeConn].  More
    41  typically it will create a network listener and call [Accept] or, for an HTTP
    42  listener, [HandleHTTP] and [http.Serve].
    43  
    44  A client wishing to use the service establishes a connection and then invokes
    45  [NewClient] on the connection.  The convenience function [Dial] ([DialHTTP]) performs
    46  both steps for a raw network connection (an HTTP connection).  The resulting
    47  [Client] object has two methods, [Call] and Go, that specify the service and method to
    48  call, a pointer containing the arguments, and a pointer to receive the result
    49  parameters.
    50  
    51  The Call method waits for the remote call to complete while the Go method
    52  launches the call asynchronously and signals completion using the Call
    53  structure's Done channel.
    54  
    55  Unless an explicit codec is set up, package [encoding/gob] is used to
    56  transport the data.
    57  
    58  Here is a simple example.  A server wishes to export an object of type Arith:
    59  
    60  	package server
    61  
    62  	import "errors"
    63  
    64  	type Args struct {
    65  		A, B int
    66  	}
    67  
    68  	type Quotient struct {
    69  		Quo, Rem int
    70  	}
    71  
    72  	type Arith int
    73  
    74  	func (t *Arith) Multiply(args *Args, reply *int) error {
    75  		*reply = args.A * args.B
    76  		return nil
    77  	}
    78  
    79  	func (t *Arith) Divide(args *Args, quo *Quotient) error {
    80  		if args.B == 0 {
    81  			return errors.New("divide by zero")
    82  		}
    83  		quo.Quo = args.A / args.B
    84  		quo.Rem = args.A % args.B
    85  		return nil
    86  	}
    87  
    88  The server calls (for HTTP service):
    89  
    90  	arith := new(Arith)
    91  	rpc.Register(arith)
    92  	rpc.HandleHTTP()
    93  	l, err := net.Listen("tcp", ":1234")
    94  	if err != nil {
    95  		log.Fatal("listen error:", err)
    96  	}
    97  	go http.Serve(l, nil)
    98  
    99  At this point, clients can see a service "Arith" with methods "Arith.Multiply" and
   100  "Arith.Divide".  To invoke one, a client first dials the server:
   101  
   102  	client, err := rpc.DialHTTP("tcp", serverAddress + ":1234")
   103  	if err != nil {
   104  		log.Fatal("dialing:", err)
   105  	}
   106  
   107  Then it can make a remote call:
   108  
   109  	// Synchronous call
   110  	args := &server.Args{7,8}
   111  	var reply int
   112  	err = client.Call("Arith.Multiply", args, &reply)
   113  	if err != nil {
   114  		log.Fatal("arith error:", err)
   115  	}
   116  	fmt.Printf("Arith: %d*%d=%d", args.A, args.B, reply)
   117  
   118  or
   119  
   120  	// Asynchronous call
   121  	quotient := new(Quotient)
   122  	divCall := client.Go("Arith.Divide", args, quotient, nil)
   123  	replyCall := <-divCall.Done	// will be equal to divCall
   124  	// check errors, print, etc.
   125  
   126  A server implementation will often provide a simple, type-safe wrapper for the
   127  client.
   128  
   129  # Security Considerations
   130  
   131  This package is not hardened against adversarial peers and is
   132  out of scope for the [Go Security Policy].
   133  
   134  [Go Security Policy]: https://go.dev/security/policylient.
   135  */
   136  package rpc
   137  

View as plain text