When I learn go language, I was confused by interface{} parameter
for example, I use the net/rpc
the interface is:
// description: Call invokes the named function, waits for it to complete, and returns its error status.
func (client *Client) Call(serviceMethod string, args interface{}, reply interface{}) error
I just pass the reply parameter as value, the program will error:rpc call error:reading body gob: attempt to decode into a non-pointer
so how to distinguish when I should pass pointer or pass value for the interface.
The interface{}
type for a function argument indicates, that you can pass value with any type to that argument. That is because all types implements interface{}
(empty interface). This also includes a pointer type as well.
Because of that, You cannot judge whether you need to pass pointer or value based on the interface{}
type declaration of that argument alone. Because It will accept both. So, you need to consult documentation of the specific function, and see what author expects.
In your case, the error is defined at https://golang.org/src/encoding/gob/decoder.go As the error says, decoder need a pointer for the reply parameter
. Otherwise, the reply
won't get back to the caller.