My First Go Project — A Virtual Filesystem

A Go project to get me started in Go and because filesystems are cool.

I needed a blue-ish background and my first background wasn’t great. :’)

So today I decided I was gonna get started in pursuing a glamorous and innovative career contributing to opensource. I’ve had my eye on Go for a long time but never really committed to really learning it, believing that C was the only low level language for me. However, there are so many interesting projects in Go across Github to sink one’s teeth into and even Go itself is like the highly intuitive and functional offspring of Python and C — the only two languages I could really do any damage in before now. It’s got that beautifully clean and easy management of data akin to Python with the pointer-y and efficiency of C — sort of; I don’t want to be quoted on that. It also doesn’t enforce OOP which is a massive plus.

So here I am, reader, ready to jump into Go by building a super awesome project that I was absolutely gonna get to writing eventually/totally not kidding… but then didn’t. I had plans to cut my teeth on a virtual filesystem in C but then I spent a year building a C compiler instead, that I then went on and refactored from Java into C (so now the C compiler was written in C) and now the language makes me feel a bit ill when I have to think of writing in it.

I thought writing a filesystem and then documenting it would be a great idea. I’d learn Go, try my hand at tutorial writing and finally cross that filesystem project off my to-do list. Well, I say filesystem, we’ll be taking a lot of liberties. The benefits of it being virtual is that all that paging and memory mapping stuff can be glazed over if not outright ignored.

So for any of you interested in following along, sit back, relax, grab your text editors and let’s set up our story because every good struggle and great protagonist needs an equally diabolical and convincing villain. So, let’s… Go (see what I did there)?

Now, The Project:

Every great project needs a great problem to stand triumphantly atop so to make things interesting, our filesystem will do a few things differently.

So here’s the origin of the problem we’ll be tackling; imagine you’re sitting there, needing to make a small copy of a small part of your filesystem to send to a friend or coworker. You could just make your changes, zip up your files and send them through. However, what if you need to make edits while still keeping a previous version of all those files and folders you’ve now tainted? That’s a lot of Ctrl+Zs you have to do to go back on all those changes. So our filesystem will address this nonexistent issue by allowing us to edit a virtual copy of a directory and all its subdirectories, all in virtual space and instantly save a copy of this edited “mini-filesystem” ready to be sent off to a friend or to the cloud. This is the premise on which everything we do will be built upon.

The ideas aren’t anything revolutionary, but apart from writing a filesystem tutorial, we’ll get our hands dirty with a lot of Go’s syntax; from linked list implementation to hashtables, data structures and the flavorful standard library I’ve heard so many good things about and with a bit of luck, we’ll have a sort of working filesystem. As a safety precaution, I recommend you build and test this code in a folder that doesn’t have a lot of subdirectories. Running this in a folder that has the Dota 2 binary would probably kill most of your RAM, so safety first. But more on that when we actually begin.

The Prep Material

As I’m writing this, I’m still preparing and getting comfortable with a lot of the techniques I’ll need to understand to pull this off in Go by doing short exercises in the language. Mainly to teach myself some concepts but also to test small versions of the larger implementation details to come; things like using a linked list to mimic directory traversal, using key/value maps to mimic the file/contents relationship and so on. Basically, there’s quite a lot I don’t think I know and I’m worried I might not be able to pull off a lot of my ideas so this practice run is a necessary detour.

The first exercise was basic linked list manipulation and directory traversal; I figure this is a great first step. This has to be completely crystal clear from the very offset or bad things can happen.

It wasn’t so bad; intimidating at first as my brain had to resist the urge to just “do it in C” and be done with it.

The code for my efforts looked something like this.

One function for each of our inevitable operations; creating a node; each node is a single directory, and a traversal system for going to and from nodes or directories.

Second on the list was basic file I/O and prompt manipulation. This will translate into making a digital copy of a directory and all its subdirectories when the filesystem is exited. Writing it to a file to save your work. And the shell loop for user input.

I suspect creating the filesystem part will be a lot easier than the actual loading and saving of information, but we’ll burn that bridge when we cross it.

Lastly, to round things off, a basic overview of the file and directory traversal will look something like this; Directories arranged as structs with their own map values. Their key/value pairs being the file names and contents respectively, and lastly an array of directory structs that will be the directories inside these directories.

The missing pieces aren’t as horrendous as I first imagined they’d be, looking through C tinted glasses. I was bracing for the absolute worst but with all my ideas working out so far, it seems like this is doable.

I’ll be documenting each step in a tutorial-esque fashion after this brief overview of the road so far.

I suspect for the longest time I’ll run into the trap of coding C in Go, but here’s to hoping I’ll be able to grow out of it as we go along.

And that’s it for our first entry. I’ll probably need to do a few more of these short exercises but I figure I won’t document the rest and just skip to beginning the project with what I have. I’ll see you soon with a part 2 as I get started on laying the groundwork and actually start coding the project out.

Till next time.

I’m a software developer by day and tinkerer by night. Working on getting into opensource stuff with a focus on C and Python. I’m also a Ratchet and Clank fan.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store