FAQ
Hello Gophers!

Do you know how to use a map typed output parameter in the implementation
of an RPC function?

I tried the following, but by running it server gives error "assignment to
entry in nil map".

Thanks.

package main

import (

     "fmt"

     "net"

     "net/rpc"

)


type RpcServer struct {

}


func (s *RpcServer) Work(_ bool, out *map[string]string) error {

(*out)["a"] = "a"

return nil

}


func main() {

     server := new(RpcServer)

     rpc.Register(server)

     sock, err := net.Listen("tcp", ":1234")

     if err != nil {

         panic(err)

     }


     go func() {

         for {

             conn, err := sock.Accept()

             if err != nil {

                 panic(err)

             }

             go rpc.ServeConn(conn)

         }

     }()

client, err := rpc.Dial("tcp", "127.0.0.1:1234")

if err != nil {

  panic(err)

}

out := make(map[string]string)

if err = client.Call("RpcServer.Work", false, &out); err != nil {

  panic(err)

}

fmt.Println(out)

}

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Rob Pike at Mar 16, 2014 at 12:48 am
    You have to create the map. It's no different from normal code, and
    the message told you the problem: the map was nil.

    func (s *RpcServer) Work(_ bool, out *map[string]string) error {
       m = make(map[string]string)
       m["a"] = "a"
       *out = m
       return nil
    }

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Howard Guo at Mar 17, 2014 at 6:43 am
    Thanks very much Rob!
    On 16 Mar 2014, at 01:47, Rob Pike wrote:

    You have to create the map. It's no different from normal code, and
    the message told you the problem: the map was nil.

    func (s *RpcServer) Work(_ bool, out *map[string]string) error {
    m = make(map[string]string)
    m["a"] = "a"
    *out = m
    return nil
    }
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 15, '14 at 8:29p
activeMar 17, '14 at 6:43a
posts3
users2
websitegolang.org

2 users in discussion

Howard Guo: 2 posts Rob Pike: 1 post

People

Translate

site design / logo © 2022 Grokbase