Last active
May 17, 2021 11:12
-
-
Save monmohan/04b309e53aa1c5065d5874238405a598 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 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