Last active
September 23, 2019 15:43
-
-
Save Azhovan/bbd66b2f53f202163ca1e353979ea17a to your computer and use it in GitHub Desktop.
chunk file into smaller pieces
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
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