Ways to read a file

Published: Tuesday, Aug 22, 2017 Last modified: Monday, Dec 9, 2024

One way

buf := make([]byte, hdr.Size)
// read file content into buffer
fReader := bufio.NewReader(file)
fReader.Read(buf)

A better way?

buf, err := ioutil.ReadAll(file)
if err != nil {
	log.WithError(err).Error("reading file")
	http.Error(w, "Error reading file.", http.StatusInternalServerError)
	return
}