Last active
May 17, 2021 13:48
-
-
Save monmohan/3e41872115c4a8ec6f57794755b924f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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