dp19001 2018-02-27 05:57
浏览 193

从Go客户端到python服务器的Protobuf消息

I want to send a message from go client to python server. I am using protobuff for the same.

Go side message structure

type CreateProductInfo struct  {
  name string
  fruits []*Fruits
}

type Fruits struct  {
 name string
}

I am expecting the below response in my python server.

{
   name : "product_info"
   fruits : [
              {
                name : "Apple"
              }
            ]
 }

Instead, I'm getting this.

 {
   name : "product_info"
   fruits : [

                name : "Apple"

            ]
 }
  • 写回答

1条回答 默认 最新

  • douping3860 2018-02-27 10:11
    关注

    If I understood your question correctly, there is no problem with rpc message transfer. Instead, you are receiving the wrong message type. Please make sure you are preparing the protobuf message in the correct format.

    package main
    
    import (
        "encoding/json"
        "fmt"
        "os"
    )
    
    type CreateProductInfo struct {
        Name   string    `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
        Fruits []*Fruits `protobuf:"bytes,2,opt,name=fruits" json:"fruits,omitempty"`
    }
    
    type Fruits struct {
        Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
    }
    
    func main() {
        productInfo := &CreateProductInfo{
            Name: "product_info",
            Fruits: []*Fruits{
                &Fruits{
                    Name: "apple",
                },
                &Fruits{
                    Name: "orange",
                },
                &Fruits{
                    Name: "mango",
                },
            },
        }
    
        b, err := json.MarshalIndent(&productInfo, "", "\t")
        if err != nil {
            fmt.Println("error:", err)
        }
        os.Stdout.Write(b)
    }
    

    This returns like this.

    {
        "name": "product_info",
        "fruits": [
            {
                "name": "apple"
            },
            {
                "name": "orange"
            },
            {
                "name": "mango"
            }
        ]
    }
    

    Go-Playground link

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器