我想从 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"
}
]
}
关于python - 从 go 客户端到 python 服务器的 Protobuf 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49002056/