Skip to content
hakk
  • Home
  • Blog
  • Docs
  • DSA
  • Snippets

Code Examples

  • Golang Listing Directory Contents and Sorting by File Name 2020-06-09

    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.DirEntry) {
    	sort.Slice(files, func(i, j int) bool {
    		return files[i].Name() < files[j].Name()
    	})
    }
    
    func SortFileNameDescend(files []fs.DirEntry) {
    	sort.Slice(files, func(i, j int) bool {
    		return files[i].Name() > files[j].Name()
    	})
    }
    
    func printFiles(files []fs.DirEntry) {
    	for _, file := range files {
    		fileInfo, err := file.Info()
    		checkErr(err)
    		fmt.Println(file.Name(), fileInfo.Size(), fileInfo.ModTime())
    	}
    }
    
    func main() {
    	files, err := os.ReadDir(".")
    	if err != nil {
    		log.Fatal(err)
    	}
    
    	//SortFileNameAscend(files)
    	printFiles(files)
    
    	fmt.Println("\n-----------------------------------\n")
    
    	SortFileNameDescend(files)
    
    	printFiles(files)
    }
    

  • Golang Listing Directory Contents and Sorting by File Size 2020-06-09

    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].Size() < files[j].Size()
    	})
    }
    
    func SortFileSizeDescend(files []os.FileInfo) {
    	sort.Slice(files, func(i, j int) bool {
    		return files[i].Size() > files[j].Size()
    	})
    }
    
    func printFiles(files []os.FileInfo) {
    	for _, file := range files {
    		fmt.Println(file.Name(), file.Size(), file.ModTime())
    	}
    }
    
    func main() {
    	files, err := ioutil.ReadDir(".")
    	if err != nil {
    		log.Fatal(err)
    	}
    
    	SortFileSizeAscend(files)
    
    	printFiles(files)
    
    	fmt.Println("\n-----------------------------------\n")
    
    	SortFileSizeDescend(files)
    
    	printFiles(files)
    }
    

  • ««
  • «
  • 1
  • 2
  • »
  • »»
Recent posts
  • Build Vim With Python3 Support
  • Understanding the ss Command: A Modern Alternative to netstat
  • Understanding HTTP from Scratch with Python Sockets
  • When Environment Variables Mysteriously Reset...
  • How to Generate a 32-byte Key for AES Encryption
© 2026 hakk
  • Home
  • Blog
  • Docs
  • DSA
  • Snippets