Skip to content

Instantly share code, notes, and snippets.

@monmohan
Last active May 17, 2021 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monmohan/3e41872115c4a8ec6f57794755b924f8 to your computer and use it in GitHub Desktop.
Save monmohan/3e41872115c4a8ec6f57794755b924f8 to your computer and use it in GitHub Desktop.
func readBookDataset(protoInFile string) error {
var rLen uint32
var book typedefs.Book
r, err := os.Open(protoInFile)
if err != nil {
return err
}
defer r.Close()
for {
bl := make([]byte, 4)
n, err := r.Read(bl)
if err == io.EOF {
break
}
if err != nil {
return err
}
//Read 4 bytes and convert to uint32 length of message
rLen = binary.LittleEndian.Uint32(bl)
//Read the message next
buf := make([]byte, rLen)
_, err = r.Read(buf)
if err == io.EOF {
break
}
if err != nil {
return err
}
//Unmarshal to Book struct
if err = proto.Unmarshal(buf, &book); err != nil {
return err
}
fmt.Printf("Title: %s \nAuthor: %s \nISBN: %s \nOverview: %s\n\n", book.Title, book.Author, book.Isbn, book.Overview)
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment