Search code examples
goprotocol-buffers

Does golang provide an easy way to output human readable protobuf


Is there a good way to get a human readable string representation of protobuf objects in golang? Something equivalent to https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.message#Message.DebugString?

I am using https://github.com/golang/protobuf.


Solution

  • I believe you're looking for proto.MarshalTextString.

    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))
    

    You can see an example in the Go package test.