hakk

software development, devops, and other drivel
Tree lined path

Code Examples

Golang Listing Directory Contents and Sorting by File Name

Listing the contents of a directory in go and sorting by filename. To get the list of file we will use the ReadDir function from the os package. To sort the files we will use the sort package to arrange the filenames alphabetically ascending or descending. package main import ( "fmt" "io/fs" "log" "os" "sort" ) func checkErr(err error) { if err != nil { log.Fatal(err) } } // this is the default sort order of golang ReadDir func SortFileNameAscend(files []fs. Read more...

Golang Listing Directory Contents and Sorting by File Size

Listing the contents of a directory in go and sorting by file size. To get the list of file we will use the ReadDir function from os package. To sort the files we will use the sort package and compare the file sizes to arrange them in ascending or descending order. package main import ( "fmt" "io/ioutil" "log" "os" "sort" ) func SortFileSizeAscend(files []os.FileInfo) { sort.Slice(files, func(i, j int) bool { return files[i]. Read more...