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

标签 python go protocol-buffers

我想从 go 客户端向 python 服务器发送消息。 我正在使用 protobuff。

Go端消息结构

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

type Fruits struct  {
 name string
}

我期待在我的 python 服务器中得到以下响应。

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

相反,我明白了。

 {
   name : "product_info"
   fruits : [

                name : "Apple"

            ]
 }

最佳答案

如果我没看错你的问题,rpc消息传输是没有问题的。相反,您收到了错误的消息类型。 请确保您以正确的格式准备 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)
}

这样返回。

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

Go-Playground link

关于python - 从 go 客户端到 python 服务器的 Protobuf 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49002056/

相关文章:

python - 将 Python Pandas 数据框相乘以获得列中值的乘积

python - 导入 matlotlibrc 文件?

networking - 以编程方式模拟数据包丢失和延迟

java - 使用 Protocol Buffer 序列化日期

c++ - 使用 g++-5 时,protobuf 不会在 osx 上编译

python - Protocol Buffers 重复字段

python - 使用 requests_html 时无法按预期提取结果

python - Python中的绿色线程和线程

go - 在 golang 中批量处理来自 ms azure eventhub 的事件

go - 在 golang 中使用 UUID 生成唯一的文件名