Skip to content

Instantly share code, notes, and snippets.

@cezary13k
Forked from AntiKnot/Makefile
Created March 24, 2026 11:49
Show Gist options
  • Select an option

  • Save cezary13k/e3590c120ff6bb013816d1c575034ffa to your computer and use it in GitHub Desktop.

Select an option

Save cezary13k/e3590c120ff6bb013816d1c575034ffa to your computer and use it in GitHub Desktop.
sample-go-rpc
package main
import (
"fmt"
"net/rpc"
)
type DoubleArgs struct {
Arg int
}
type DoubleReply struct {
Res int
}
func main() {
client, err := rpc.DialHTTP("tcp", "localhost:8081")
if err != nil {
panic(err.Error())
}
args := DoubleArgs{Arg: 1,}
var reply *DoubleReply
err = client.Call("MathUtil.Double", args, &reply)
if err != nil {
panic(err.Error())
}
fmt.Println(reply.Res)
}
run-server:
go run server/server.go
run-client:
go run client/client.go
package main
import (
"net"
"net/http"
"net/rpc"
)
type MathUtil struct {
}
type DoubleArgs struct {
Arg int
}
type DoubleReply struct {
Res int
}
func (mu *MathUtil) Double(args DoubleArgs, reply *DoubleReply) error {
reply.Res = 2 * args.Arg
return nil
}
func main() {
mathUtil := new(MathUtil)
err := rpc.Register(mathUtil)
if err != nil {
panic(err.Error())
}
rpc.HandleHTTP()
listen, err := net.Listen("tcp", "localhost:8081")
if err != nil {
panic(err.Error())
}
http.Serve(listen, nil)
}
.
├── Makefile
├── client
│   └── client.go
└── server
└── server.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment