refactor: empty file finder lecture 3

This commit is contained in:
Inanc Gumus 2019-03-06 18:24:47 +03:00
parent a9e327c36b
commit c05eed3c95
2 changed files with 11 additions and 11 deletions

View File

@ -32,6 +32,7 @@ func main() {
if file.Size() == 0 {
name := file.Name()
fmt.Println(cap(names))
names = append(names, name...)
names = append(names, '\n')
}

View File

@ -13,15 +13,6 @@ import (
"os"
)
// You can easily write to a file using bash.
//
// However, when what you're creating is a library,
// then you won't have that option. So, it's good to learn
// how to write to a file using a byte slice.
// ioutil.ReadDir
// os.FileInfo
func main() {
args := os.Args[1:]
if len(args) == 0 {
@ -35,15 +26,23 @@ func main() {
return
}
// 1st: find the total size of all the empty files
// 1st: You can also take the average of the total file length
// across platforms. It's about 255.
//
// https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits
//
// total := len(files) * 256
// 1st B: To be exact, find the total size of all the empty files
var total int
for _, file := range files {
if file.Size() == 0 {
// +1 for the newline character
// when printing the filename
// when printing the filename afterward
total += len(file.Name()) + 1
}
}
fmt.Printf("Total required space: %d bytes.\n", total)
// 2nd: allocate a large enough byte slice in one go
names := make([]byte, 0, total)