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

Docs

  • 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)
    }
    

  • Initial Ubuntu Server Setup

    2020-06-09

    When deploying a new Ubuntu server there are a few steps that should be taken in order to make sure it’s secure. This should provide a fairly secure default setup. However, as more software is added further measures will need to be taken in order to secure the server.

    Bare Metal Installations

    Note: It is now an option to install openssh during installation.

    Originally I wrote this tutorial for setting up a new server on a cloud hosting provider. Those already come setup with an ssh server; but if you’ve downloaded your Ubuntu from the site and are setting it up on a bare metal server, it needs an extra step.

  • ««
  • «
  • 1
  • 2
  • 3
  • »
  • »»
Recent posts
  • 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
  • Streamlining Deployment: Installing Docker, Gitea, Gitea Act Runner, and Nginx on Ubuntu
© 2025 hakk
  • Home
  • Blog
  • Docs
  • DSA
  • Snippets