FAQ
Hello Gophers!

While using Go RPC, I feel very confused about the memory management
mechanism used by both the RPC server and client.

Take a look at the following three server function signatures:

type MyData struct {}
func Fun1(in int, out *MyData) error
func Fun2(in int, out *[]int) error
func Fun3(in int, out *map[string]int) error

By trial and error, I figured out:
- To use Fun1:
Server - no need to allocate memory for MyData
Client - need to allocate memory new(MyData)
- To use Fun2:
Server - need to initialise []int with desired size
Client - need to make an empty []int, no initialisation
- To use Fun3:
Server - needs to initialise (make) map[string]int
Client - needs to initialise (make) map[string]int (or not?!)

First: I cannot to get Fun3 to work -
if server does not initialise output, server complains for nil pointer;
if client does not initialise output, the call succeed(!) but client
complains for nil pointer when trying to access the output;
if both client and server initialise the output, and server puts a key into
the map, the call succeed but client sees nothing in the output!
So how to get it working?

Second: How does RPC client deserialize []int output? Does RPC library
make([]int, 0, 0) and then put all integers into the slice?

Third: If MyData contains a map[string]int and []int, I presume the server
is responsible for making the map[string] and []int, however: when client
allocates memory for MyData, does it need to also initialize the map and
slice?

Thanks.

Regards,
Howard

--
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

  • Volker Dobler at Apr 21, 2014 at 11:25 pm
    Could you post a full example on the playground?

    Maybe it is just some tiny misconception, e.g. on the server
    you never should have to allocate memory for the out
    parameter, but of course you cannot fill a nil slice or
    map, so it should be pretty clear that you'll have to
    "make" slices and maps on the server before stuffing
    values in. But that has nothing to do with package rpc,
    it is the way slices and maps work.
    For the client: If you want to send the zero value of
    a slice or map, you do not "make" them (because they
    wouldn't be nil). Again: If you want to populate the slice
    or map you need a non-nil slice or map: Either by
    making them, using a literal or reusing an existing one.

    V.


    Am Sonntag, 20. April 2014 11:41:57 UTC+2 schrieb Howard Guo:
    Hello Gophers!

    While using Go RPC, I feel very confused about the memory management
    mechanism used by both the RPC server and client.

    Take a look at the following three server function signatures:

    type MyData struct {}
    func Fun1(in int, out *MyData) error
    func Fun2(in int, out *[]int) error
    func Fun3(in int, out *map[string]int) error

    By trial and error, I figured out:
    - To use Fun1:
    Server - no need to allocate memory for MyData
    Client - need to allocate memory new(MyData)
    - To use Fun2:
    Server - need to initialise []int with desired size
    Client - need to make an empty []int, no initialisation
    - To use Fun3:
    Server - needs to initialise (make) map[string]int
    Client - needs to initialise (make) map[string]int (or not?!)

    First: I cannot to get Fun3 to work -
    if server does not initialise output, server complains for nil pointer;
    if client does not initialise output, the call succeed(!) but client
    complains for nil pointer when trying to access the output;
    if both client and server initialise the output, and server puts a key
    into the map, the call succeed but client sees nothing in the output!
    So how to get it working?

    Second: How does RPC client deserialize []int output? Does RPC library
    make([]int, 0, 0) and then put all integers into the slice?

    Third: If MyData contains a map[string]int and []int, I presume the server
    is responsible for making the map[string] and []int, however: when client
    allocates memory for MyData, does it need to also initialize the map and
    slice?

    Thanks.

    Regards,
    Howard
    --
    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
postedApr 20, '14 at 9:42a
activeApr 21, '14 at 11:25p
posts2
users2
websitegolang.org

2 users in discussion

Howard Guo: 1 post Volker Dobler: 1 post

People

Translate

site design / logo © 2022 Grokbase