Skip to content

Instantly share code, notes, and snippets.

@neepoo
Created August 15, 2023 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neepoo/f35bf68ee0b341d14411af08d3fbd6d6 to your computer and use it in GitHub Desktop.
Save neepoo/f35bf68ee0b341d14411af08d3fbd6d6 to your computer and use it in GitHub Desktop.
Calculate the size of the specified file type in the directory
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