2013-07-17 18:23
浏览 130


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 (

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()
    l, err := net.Listen("tcp", ":1234")
    if err != nil {
        log.Fatal("listen error:", err)
    for {
        conn, err := l.Accept()
        if err != nil {

No matter what I tried, I've always got the following response:


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


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongshi2141 2013-07-17 18:47

    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:

    • Construct a new string with a new value.
    • Let reply point to this new string

    This lets the string which is returned completely unaffected. Try

    *reply = s
    打赏 评论

相关推荐 更多相似问题