Viswa Kumar
  • Home
  • About
    • My Background
    • My reading list
  • Research
    • Research Overview
    • Publications
  • Writing
    • Blog
    • Newsletter
    • Papershelf
    • Book Notes
  • Projects

On this page

  • Intrigued to learn more about my lsgo project??
    • Usage
    • Sample output

lsgo Implementing ls command in golang

golang
programming
projects
Substack
Throwbacks
Implementing ls command in golang for fun & learning
Published

April 22, 2023

Throwback Posts ⭐️

Whenever you see a post with this callout, it means I had ported some of my old (but still close to heart) post from my old/legacy blog host to here. Enjoy some good old classics ☕️

Although it appears that I’m late to the Gophers party, I have finally managed to embrace Idiomatic Go in my programming arsenal.

I now realized that better late than never. Golang is truely a highly productive language.

In this post, I’m gonna talk about my new project lsgo - ls command in golang for fun & learning.

After watching various Gophercon videos , Rob Pike’s “Concurrency is not Parallelism” & Liz’s “Containers from Scratch” , I was super charged in getting my hands dirty with golang.

Rather than starting out in traditional hello-world world, I decided to take a spin and wanted to try with real-world program.

Well, who doesn’t know about ls command?? . Simple, yet powerful. So what about, implementing ls command from scratch using golang ? and how much time does a newbie like me, would typically need in converting an idea to implementation, using golang ??

As it turns out, it only took me a weekend to learn golang basic & it’s powerful idioms, there by implementing a subset of mighty ls command purely in golang.

The power of golang is such that, what started out as something very trivial as below

func serveDir(dir string) {
    f, err := os.OpenFile(dir, os.O_RDONLY, 0666)
    checkerr(err)
    files, err := f.Readdirnames(0)
    checkerr(err)
    for _, file := range files {
        fmt.Println(file)
    }
}

ramped up into a complete tool within short span of time ( read productivity ). No wonder why every start-up is thriving with golang .

Needless to say, the experience was so much fun & absolutely addictive. I’m sure I’m gonna use golang for all my future coding endeavours.

Intrigued to learn more about my lsgo project??

lsgo is available in Github, also listed in my Projects page. While not all features of ls isn’t available, this tool serves PWD & any directory when provided as an input.

Along with normal file listing, this tool also supports long, size in human readable format, file-only, dir-only listing. Furthermore, it also supports tree lookup (recursive directory lookup) .

The goal is not to replace ls, rather than to mimic ls in golang to the extent possible using native golang constructs - basically for fun & learning golang.

Usage

 ./ls --help

NAME:
   ls ( implemented in golang ) - ls [flags] [command][args]

USAGE:
   ls [global options] command [command options] [arguments...]

VERSION:
   0.1

AUTHOR:
   Viswa Kumar

COMMANDS:
     help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   -l, --long             include extended information
   -d, --dronly           include only directories
   -f, --fileonly         include only regular files
   --hr, --humanfriendly  view size in humar readable format
   -t, --tree             view tree structure ( recursive lookup )
   --help, -h             show help
   --version, -v          print the version

COPYRIGHT:
   MIT Licensed

Sample output

./ls -hr
+-----------+--------+------------+---------------------+-------+
|   NAME    |  SIZE  |   PERMS    |         AT          |  DIR  |
+-----------+--------+------------+---------------------+-------+
| .git      | 4K     | drwxrwxrwx | 2017-10-22 01:15:14 | true  |
| demo.json | 116.5K | -rwxrwxrwx | 2017-10-22 01:02:45 | false |
| LICENSE   | 1.1K   | -rwxrwxrwx | 2017-10-21 19:34:57 | false |
| ls        | 3.5M   | -rwxrwxrwx | 2017-10-22 00:31:26 | false |
| ls.go     | 3.2K   | -rwxrwxrwx | 2017-10-22 00:31:23 | false |
| README.md | 166B   | -rwxrwxrwx | 2017-10-22 01:11:07 | false |
+-----------+--------+------------+---------------------+-------+

If you happen to like my project, feel free to let me know your thoughts and I would love to hear them. Cya in my next blogpost..

  • A learning Gopher…
Subscribe to Techno Adventure Newsletter

I also publish a newsletter where I share my techo adventures in the intersection of Telecom, AI/ML, SW Engineering and Distributed systems. If you like getting my post delivered directly to your inbox whenever I publish, then consider subscribing to my substack.

I pinky promise 🤙🏻 . I won’t sell your emails!

Subscribe ✉️

 

Copyright 2024, Viswa Kumar