I've coded a simple JSONRPC server to test Go's capabilities, but I'm stuck, always getting an empty result, without error and correct id. I've got the following Go code: package main
import (
"log"
"net"
"net/rpc"
"net/rpc/jsonrpc"
)
type Experiment int
func (e *Experiment) Test(i *string, reply *string) error {
s := "Hello, " + *i
reply = &s
log.Println(s, reply)
return nil
}
func main() {
exp := new(Experiment)
server := rpc.NewServer()
server.Register(exp)
l, err := net.Listen("tcp", ":1234")
if err != nil {
log.Fatal("listen error:", err)
}
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
server.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}
No matter what I tried, I've always got the following response:
{"id":1,"result":"","error":null}
Logging shows everything is working as it should in the server.
2013/07/17 15:17:13 Hello, Paulo 0xc200090ac0
Any ideas on what's going on? I'm using the latest stable version of Go, v1.1.1
Thanks
Your reply is of type pointer to string. That is okay and even required as the second argument is used to return the answer. But then you do:
s := "Hello, " + *i
reply = &s
Which translates to:
This lets the string which is returned completely unaffected. Try
*reply = s