Newer
Older
package util
import (
"fmt"
"github.com/golang/protobuf/proto"
"io/ioutil"
)
/*
Copycat source: https://dev.to/techschoolguru/go-generate-serialize-protobuf-message-4m7a
// Write writes protocol buffer message to binary file
func Write(message proto.Message, filename string) error {
data, err := proto.Marshal(message)
if err != nil {
return fmt.Errorf("cannot marshal proto message to binary: %w", err)
}
err = ioutil.WriteFile(filename, data, 0644)
if err != nil {
return fmt.Errorf("cannot write binary data to file: %w", err)
}
return nil
}
func Read(filename string, message proto.Message) error {
data, err := ioutil.ReadFile(filename)
if err != nil {
return fmt.Errorf("cannot read binary data from file: %w", err)
}
err = proto.Unmarshal(data, message)
if err != nil {
return fmt.Errorf("cannot unmarshal binary to proto message: %w", err)
}
return nil