DEV Community

Cover image for Created a URL Router called goblin in Golang
Kenta Takeuchi
Kenta Takeuchi

Posted on • Originally published at bmf-tech.com

Created a URL Router called goblin in Golang

This article was originally published on bmf-tech.com.

Overview

I created a URL router in Golang, so I will note down the process of implementation.

Preparation

Here are the preparations I made when implementing the URL router.

Data Structures and Algorithms

I considered the logic of how to match URLs.

Many libraries often use tree structures as data structures, so I thought about what type of tree structure to adopt.

Among trees specialized for string searching, it seemed that a radix tree would be the best choice in terms of time and memory complexity, so I initially tried to adopt that, but I gave up due to its complexity.

I decided to use a trie tree for something a bit more familiar and simpler.

Reading net/http Code

To implement it as an extension of the multiplexer in net/http, it is necessary to have a certain understanding of its internal mechanisms.

Refer to Reading Golang's HTTP Server Code.

Reading Various Router Implementations

Reference > See the repository.

Others

An article summarizing past URL routing.

URL Routing Articles

Implementation

Refer to github.com - goblin.

The basic idea is to transform the trie tree into a more usable form, but I struggled several times with handling path parameters. The handling of regular expressions was not too troublesome, as it could be managed by simply preparing a DSL, so the sense of handling the DSL was put to the test.

During the implementation process, I wrote tests in parallel and repeated step-by-step debugging, which helped me catch how the data structure was constantly changing, so I felt my mental debugging skills were improving.

Since this involved logic I don't usually write, it surely became good training for coding.

The future challenges are as per the issues raised in the repository, and I plan to refine them when I have some free time.

References

Repository

Repositories that I referred to for design and implementation.

Others

Articles referenced during implementation.

Top comments (0)