golang是否提供了一种简单的方法来输出人类可读的protobuf

golang是否提供了一种简单的方法来输出人类可读的protobuf,go,protocol-buffers,Go,Protocol Buffers,在golang中,有没有一种好方法可以获得protobuf对象的可读字符串表示?相当于什么 我正在使用。您可以使用。使用稍微修改的示例proto: p := &example.Test{ Label: proto.String("this"), Reps: []int64{4, 3, 2, 1}, InnerTest: &example.Test_InnerTest{ InnerLabel: proto.String("is the en

在golang中,有没有一种好方法可以获得protobuf对象的可读字符串表示?相当于什么

我正在使用。

您可以使用。使用稍微修改的示例proto:

p := &example.Test{
    Label: proto.String("this"),
    Reps:  []int64{4, 3, 2, 1},
    InnerTest: &example.Test_InnerTest{
        InnerLabel: proto.String("is the end"),
    },
}

t := proto.TextMarshaler{}
t.Marshal(os.Stdout, p)
产出:

label: "this"
reps: 4
reps: 3
reps: 2
reps: 1
inner_test: <
  inner_label: "is the end"
>
标签:“此” 代表:4 代表:3 代表:2 代表:1 内部测试:< 内部标签:“是终点” >
我相信您正在寻找


您可以在Go软件包中看到一个示例。

您是否同意对对象进行解组,还是要求您在仍然是profobuf消息时打印它?你也在使用这个软件包吗?是的,我将其解组(proto.Message对象,而不是字符串/字节[])。我更喜欢使用proto.Message,但这不是必需的。我正在使用该库。希望我能给你一个答案,因为我确信这是可能的,但我只是在大约一年的时间里没有使用该包,没有时间去挖掘它的源代码。你正在使用哪个protobuf实现?-还更新了问题
p := &example.Test{
  Label: proto.String("this"),
  Reps:  []int64{4, 3, 2, 1},
  InnerTest: &example.Test_InnerTest{
    InnerLabel: proto.String("is the end"),
  },
}

fmt.Println(proto.MarshalTextString(p))