Skip to content

Instantly share code, notes, and snippets.

@monmohan
Last active May 17, 2021 11:12
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/04b309e53aa1c5065d5874238405a598 to your computer and use it in GitHub Desktop.
Save monmohan/04b309e53aa1c5065d5874238405a598 to your computer and use it in GitHub Desktop.
func serializeBookDataset(protoOutFile string) error {
w, err := os.OpenFile(protoOutFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer w.Close()
csvFile, err := os.Open("books.csv")
if err != nil {
return err
}
defer csvFile.Close()
r := csv.NewReader(csvFile)
count := 0
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return err
}
out, err := proto.Marshal(&typedefs.Book{Title: record[1], Author: record[2], Isbn: record[9], Overview: record[19]})
if err != nil {
return err
}
//Encode length as []byte and append the message []byte
l := uint32(len(out))
buf := make([]byte, 4)
binary.LittleEndian.PutUint32(buf[0:], l)
buf = append(buf, out...)
_, err = w.Write(buf)
if err != nil {
return err
}
count++
}
fmt.Println("Total Records written ", count)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment