简体   繁体   English

从 go 客户端到 python 服务器的 Protobuff 消息

[英]Protobuff message from go client to python server

I want to send a message from go client to python server.我想从 go 客户端向 python 服务器发送消息。 I am using protobuff for the same.我正在使用 protobuff。

Go side message structure Go侧消息结构

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

type Fruits struct  {
 name string
}

I am expecting the below response in my python server.我期待在我的 python 服务器中得到以下响应。

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

Instead, I'm getting this.相反,我得到了这个。

 {
   name : "product_info"
   fruits : [

                name : "Apple"

            ]
 }

If I understood your question correctly, there is no problem with rpc message transfer.如果我正确理解了您的问题,则 rpc 消息传输没有问题。 Instead, you are receiving the wrong message type.相反,您收到了错误的消息类型。 Please make sure you are preparing the protobuf message in the correct format.请确保您正在以正确的格式准备 protobuf 消息。

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去游乐场链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM