Posts

Showing posts with the label Golang

Golang Append

While working on some code base, I found out a weird quirk of append built-in function in Golang.

There was a string slice defined and after some operations, the function was returning two things, a string and a string slice.
... s := []string{"a", "b", "c", "d"} ... return s[1], append(s[:1], s[1+1:]...) The output is supposed to be "b" and "a", "c", "d" but what I got was "c" and "a", "c", "d". At first I thought I have discovered a bug but after reading the documentation it became clear that it is intentional. Turns out it has to do with implementation of Slice described in this blog. So, append modifies the underlying array and since arguments are evaluated from right to left, append is executed first which modified the underlying array to become "a", "c", "d" and then s[1] returns the 1st element of the modified array "c"…

Vanity Imports in Golang

The imports in Golang are the fullpath of the package including the domain name. It is common to host the packages on Github and use it for import as well. However, there are two problems with it.
Long imports: Packages under Github have to have github.com/<username>/<repository-name> at the beginning which is rather long considering some repositories have nested sub-directories. This is true for internal packages in the repository as well, when you want to import a package from a different sub-directory but you still have to use the entire Github prefix. Hassle in switching the hosting The package name is the unique identifier for the package and having the hosting provider name in it means its a hassle to switch provider even for internal packages.
The two aforementioned problems can be solved using vanity imports in Go. What vanity import means is using a custom domain for package imports.
github.com/<username>/<repository>/pkg/<xyz> -> <user.t…

Golinks

This project is a very simple webapp inspired by Google's internal “go” system. First time I came to know about this was from this Gitlab thread. Also from the same thread I came to know about an open-source implementation of it at kellegous/go.

I have been working with Golang extensively at work and otherwise for the past year but this is my first web application in Go. The application is analogous to a URL shortner with the different that you get to choose the slug. It implements the functionality of adding and deleting as well as listing all links. I also semi-implemented authentication where it assumes an external system (middleware) to do it and set a specific HTTP header. I am using Cloudflare Access for implementing this in my current setup.

I used Mux router to build the endpoints with two middlewares for logging and authentication. For logging, I used a leveled logging library by Netflix engineer called zerolog. I used Go implementation of LevelDB which is an embedded …

Miniflux

On one of my explorations for Selfhosted software, I came across Miniflux. It is a very minimal feed reader web application. On little research I found out that it is a Golang rewrite of Miniflux v1 originally written in PHP. I have a sweet spot for Golang software specially web applications. Setting them up is really just a matter of installing binary and setting up a system service. I decided to give it a try so I went on the website and turns out the developers provide a hosted solution for just $15/year with a 15-day trial.

I registered on the hosted version to try it out. I really liked the minimal interface and utilitarian appearance which gets out of the way. It also comes with built-in integration with a lot of Read It Later services including Pocket, Instapaper and Wallabag.

After making my mind I decided to install it on the server. It needs PostgreSQL so I installed it from Debian repository and followed the installation guide. After setting it up, I created a sub-domain…