Skip to content

Instantly share code, notes, and snippets.

@Azhovan
Last active September 23, 2019 15:43
Show Gist options
  • Save Azhovan/bbd66b2f53f202163ca1e353979ea17a to your computer and use it in GitHub Desktop.
Save Azhovan/bbd66b2f53f202163ca1e353979ea17a to your computer and use it in GitHub Desktop.
chunk file into smaller pieces
package util
// chunk the file into smaller pieces
func Chunks(path string) (parts uint64, filepath string) {
file := openFile(path)
defer closeFile(file)
fileInfo := fileInfo(file)
// simply, file 's size is divided by
// pre-defined chink size
size := fileInfo.Size()
parts = uint64(math.Ceil(float64(size) / float64(FileChunk)))
return parts, path
}
// get the basic information about file
// proper error handled
func fileInfo(file *os.File) os.FileInfo {
fileInfo, err := file.Stat()
if err != nil {
log.Fatalln(ErrFileInfoNotAvailable, err)
}
return fileInfo
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment