An argument in an RPG golang call

In the RPC handler, I omit the first argument, for example:

func (self Handler) GetName(int, reply *StructObj) {
}

and in the caller

var reply StructObj
client.Call("Handler.GetName", 0, &reply)

Since I do not need the first argument of the GetName method, I omit its name, HOWEVER, I got:

gob: type mismatch in decoder: want struct type

I changed the GetName method to GetName (id int, reply * StructObj) and it works. I want to know why this happened?

+4
source share
1 answer

You find yourself in a complex aspect of the function definition syntax in Go. You cannot have an argument without a name, and you can name the argument int, but func f(x, y, z Type)a label to declare all three type variables Type. For example, it func f(int, x string)declares connectively fthat takes two lines, one of which is called int.

package main

import "fmt"

func f(int, x string) {
    fmt.Println("int is:", int)
    fmt.Println("x is:", x)
}

func main() {
    f("foo", "bar")
}

,

int is: foo
x is: bar

, . , , , , , , , .

, , int *StructObj, *StructObj int reply. , net/rpc , 0, a *StructObj. .

+6

All Articles