I am new to GOlang. I have protobuff binary message file which has multiple messages (stream). Able to decode that using Python easily like below, but could not find any simple way to achieve same using GO. Any help would be much appreciated.
Python code:
def read_pb_stream_from_file():
amw = my_stream_pb2.MyTestMessageWrapper()
count = 0
with open("stream.strmpb",'rb') as f:
data = f.read()
n = 0
while n < len(data):
msg_len,new_pos = _DecodeVarint32(data,0)
n = new_pos
msg_buf = data[n:n+msg_len]
n += msg_len
amw.ParseFromString(msg_buf)
data = data[n:]
print(count)
print(amw)
count+=1
Found that there is util package for this golang -
Portion of code will be like :
Import "github.com/matttproud/golang_protobuf_extensions/pbutil"
data, err := ioutil.ReadFile("stream.strmpb")
myTestMessage := &my_stream.MyTestMessageWrapper{}
in := bytes.NewReader(data)
// Split proto stream to individual message
for {
myTestMessage.Reset()
if _, err := pbutil.ReadDelimited(in, myTestMessage); err != nil {
if err == io.EOF {
break
}
}
// Convert protobuf message to json
marshaler := jsonpb.Marshaler{}
jsonMessage, _ := marshaler.MarshalToString(myTestMessage)
log.Println("jsonMessage:", jsonMessage)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.