Created
August 15, 2023 05:35
-
-
Save neepoo/f35bf68ee0b341d14411af08d3fbd6d6 to your computer and use it in GitHub Desktop.
Calculate the size of the specified file type in the directory
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 main | |
import ( | |
"fmt" | |
"os" | |
"path/filepath" | |
"strings" | |
) | |
func main() { | |
if len(os.Args) < 2 { | |
panic("usage: cal_size dir <filetype>") | |
} | |
dir := os.Args[1] | |
var ( | |
size int64 | |
filter string | |
) | |
if len(os.Args) == 3 { | |
filter = os.Args[2] | |
} | |
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { | |
if info.IsDir() { | |
return nil | |
} | |
if strings.HasSuffix(path, filter) { | |
size += info.Size() | |
} | |
return nil | |
}, | |
) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("size: %d Bytes\n", size) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment