Sazid

Devlog - 2021

My personal devlog.

You can subscribe to this page via RSS.

This page lists entries for the year 2021, for past entries consult the devlog archive.

June 22, 2021

Working with fs in Go

TLDR; use path.Join when working with io/fs.FS instead of filepath.Join. Always read the docs before you start using some the codes - specially if you’re dealing with os specific resources.

Quote from doc:

Note that paths are slash-separated on all systems, even Windows. Paths containing other characters such as backslash and colon are accepted as valid, but those characters must never be interpreted by an FS implementation as path element separators. - https://golang.org/pkg/io/fs/#ValidPath


Experience:

I have been recently working on https://git.io/node_manager this project mostly from Linux. Some of the code uses the newly introduced (1.16) io/fs package - a read-only abstraction of a filesystem. There’s around 30 unit/integration tests between the different parts of the module and all of them were passing.

However, as I pulled the code on my Windows system today and re-ran the whole test suite, I quickly discovered that some of the tests were failing - the ones that interacted with the io/fs.FS. Debugging through the code, I found that filepath.Join was joining the path segments with \, I suddenly remembered that io/fs works with only / (forward slashes, even on Windows). Had I not read the doc before, this may have cost me a few hours of useless fiddling around/debugging.


PS. I highly recommend trying out the fstest package as well if you’re testing io/fs stuffs. And do use the io/fs package if all you’re doing is reading from files/dirs. This is such a nice abstraction - the backend filesystem can live on a single file/local filesystem/database/ftp/cloud storages/s3/etc.

More reading:

January 01, 2021

Rusty Stuffs

Hello, 2021!

I’ve been experimenting with Rust for a while and I really like the language. I like how it forces me to check for all the possible values when I’m matching on something or checking a condition. Compile time checks, although quite annoying at times (because of my inexperience obviously) is so.. so much better than a dynamically typed language. I no longer mistakenly get variable undefined errors or a missing comma/semi-colon error at runtime only if the code gets inside a certain condition! There are so much things to like about the language honestly. Some of my favorites are:

There are obviously many more, but tbh I’m not really that much of an expert in Rust yet so I can’t say more. There are also challenges that I have not faced yet that many experienced and big projects face. I also haven’t really grasped the lifetimes concept to the core.

With this limited knowledge, I set out to build a Websocket server that lets us display logs in real time. The client software that runs on one machine must be able to send logs to a browser that is possibly running on another machine. The client machine cannot host a server directly because of security reasons. So, the flow of messages will be: Client -> Server -> Browser. I was able to successfully build this system, however its yet to be put into production and test. Maybe I’ll write about this more about this in another post?