Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法将context.context用作golang中的gob RPC调用参数_Go_Rpc_Gob - Fatal编程技术网

无法将context.context用作golang中的gob RPC调用参数

无法将context.context用作golang中的gob RPC调用参数,go,rpc,gob,Go,Rpc,Gob,我试着用Go编写一个服务器代码。(请参阅此) 但是,如果我想在RPC调用参数中使用context.context,我发现有问题 // Init of my RPC server kv := new(KVRaft) rpcs := rpc.NewServer() rpcs.Register(kv) .... //My RPC call function func (kv *KVRaft) Msg(args *context.Context, reply *MsgReply) error {

我试着用Go编写一个服务器代码。(请参阅此)


但是,如果我想在RPC调用参数中使用
context.context
,我发现有问题

// Init of my RPC server
kv := new(KVRaft)
rpcs := rpc.NewServer()
rpcs.Register(kv)
....


//My RPC call function
func (kv *KVRaft) Msg(args *context.Context, reply *MsgReply) error {
    log.Println("[MSG]", *args)
    return nil
}
代码可以编译,但当我尝试运行

c, _ := rpc.Dial("tcp", ":1234")
var reply MsgReply{}

c.Call("KVRaft.Msg", someContext, &reply)
它将弹出错误,如
gob:type未为interface:context.emptyCtx注册

如果我将
*context.context
替换为
*string
,则此代码运行良好。 我在谷歌上搜索到它可以使用
gob.Register()
,但在
gob.Register(context.context{})上编译失败

知道如何将
context.context
作为gob RPC调用参数传递吗

[更新]

我尝试使用
gob.Register(context.Background())
,但在接收RPC调用时,它将获得空上下文

[更新]
当前我的任务在这里,但仍在进行中。

context.context
是一个接口,而不是结构。您无需为gob注册接口即可对其进行编码。您应该注册要放在上下文中的类型,并对其进行编码,将它们放在远程端的新上下文中。@elithrar谢谢,我尝试使用gob.encode,但在
gob:type未为interface:context.emptyCtx注册时仍然出错。还有什么方向吗?我是如何找到编码的方法的?
invalid type for composite literal: "golang.org/x/net/context".Context