Go(lang) file management

Cheikh seck
3 min readJul 26, 2022

--

I jumped on the Go train in 2015. I was developing a backend for a FinTech startup (Orenda) and I needed a light weight program to initiate certain backend functionality. This functionality consisted of establishing a connection with Interactive Brokers (IB) and perform calculated stock trades. IB had a Java client at the time. You may be wondering “Why not Tomcat?” I avoided Tomcat to keep the startup’s hosting costs low. Go was ideal because I could setup a virtual terminal to run the IB Java client. It also had low memory and disk space requirments. At the time, developing on Go felt like writing a bash script that could listen on a network port. While I was writing the backend service, I decided to create TheStrukture/IDE. I felt confident after writing a backend service for Orenda, and I wanted to further explore Go. At the time, there wasn’t much support for Go in code editors, so it was a “no-brainer” to develop an environment to write Go in. I made the rookie mistake of diving into the project without understanding the STD library fully, as a result, I had to put in extra hours of work.

Cross-platform compatibility

When I started writing the IDE, I only wanted support for UNIX/Linux systems alike, until I realised the majority of computers are running on Windows. Upon said realisation I booted up a Windows virtual machine to test out the IDE. To my surprise, nothing worked, even the terminal. But this is due to the hacky methods I had in place to carry out functionality. Let’s start with directories, this was the code I had in place to remove directories : (core is library I wrote and is not part of the STD lib)

core.RunCmdB("rm " + gp + "/src/" +  r.FormValue("pkg") )

The issue with this code is that Windows does not have command rmand directories are delimited by back slashes. I kept the forward slashes and used package os to handle file removals. The slashes are automatically handled for Windows.

os.Remove( "rm " + gp + "/src/" +  r.FormValue("pkg") )

To achieve multi-platform path generation I found package filepathto be very useful. The code above is not memory efficient, for each string appended with a plus sign, the program will create a new variable. These sub variables are missed by GC and result in memory leaks. So imagine, each time a request occurs, your program’ s memory will increase by 1 mb. Here is an updated version of the code above that is memory and cross-platform safe :

os.Remove( filepath.Join(gp, "src", r.FormValue("pkg") ) )

I’m using STD lib package filepath to join the strings in a safe manner. This is an assumption that I’ll make to keep my focus on developing my application.

The lesson

The lesson here is to use the STD library for performing file operations, regardless of how mundane the experience is. In the case of TheStrukture, it brought true cross-compatibility to users, as well as reduce the number of memory leaks in the program. If you haven’t yet, head over to https://github.com/thestrukture/IDE .We’re looking for sponsors to take this project to the next level. The next level being an editor that understands you, rather pulling people’s untested code from the internet and slapping it in your program.

--

--