<h1>Go Gin vs HttpRouter(Update)</h1>

Suprised!

I made an update on my tools.zh-code.com and observed something fun and interesting.

HttpRouter:

Gin:

I guess I need to test to figure it out…

I was thinking this would be something may be a problem. However, I am really satisfied with my result and I do think this is a really fun experience.

BTW, you can bind custom HTMLRenderer to gin, since gin by default only renders one template. So I binded html/template as the HTMLRenderer and it’s working just fine.

<h1>Gonic Gin vs HttpRouter</h1>

TL:DR: I prefer HttpRouter.

Story begins:

Okay… It’s been not a really long story at this point. Recently, I am working on a website for apps that I wrote and will use frequently and personally… If you are interested, please take a look at https://tools.zh-code.com

I was using HttpRouter with html/template to render pages and taking api requests.

Yesterday, I was wondering about swagger UI for the APIs and make the apps I wrote open by API requests as well. To start, I asked Dr.Google to see if there are any open source golang libraries can generate Swagger documentation on the fly based on comments. Swaggo seems to be a really interesting idea, and then another post catches my eyes Gin-Swaggo.

Gin was something I really wanted to try and it looks much more powerful then HttpRouter. However, there is a catch, performance. I know, the 20% difference will never be the bottleneck, especially for routers. Most of the cases, the bottleneck will be database. However, 20% of performance trade for a better features, I would need to think about it. As part of now, there are basically no traffic to my site except me, so I should have no problem even using .Net(I am working as a fullstack .Net developer so I can just joke myself).

This morning, I gave Gin a shot. Since the FunApps website is still new, so I was thinking why don’t I convert it to Gin. Let’s start.

1. Converting the main function to use gin.Default() instead of httprouter.New()

Okay. Not a big deal, few lines of code change, just rename it. And see what happens next…

2. Change all functions to use (c *gin.Context)

Okay. Not too bad, not too bad. Still can do it with bulk replacing. After doing bulk replace, 200+ errors thrown. Okay, okay. I am expecting that.

3. Change previously handler functions to gin’s middleware

There are some digging into the syntax of how to use Context. I have to say, it’s super convenient. I wouldn’t expect something like this would amazed me so much. I love it! Okay, gin is the way to go now! (Too soon…)

4. Let’s load all the html templates to use gin’s router

Done.

5. Moment of truth.

Nested templates are not rendered correctly…. I have headers and footer in separate templates and container as the content template. Previously, I just need to execute content template. But now, it only renders the content page itself, which result to plain txt as html…

I went to search for what I did wrong about that one, however, nothing really explaining the reason to me clearly, and it’s close to the time to start doing my job remotely… Let’s hold on it for now. I am really want to give it another try next few days, but the question will be, why? After digging into gin, I had an idea what are the good things about gin. The context, and powerful middle wares. Why can I make it using httprouter? Also, I can only implement things I need from scratch and the performance will be just fine.

Yep. I still prefer more of httprouter to implement features only what I need. Even this takes time.

Hope this will help you to save some of your time. TBH, try gin, it’s awesome. However, if you want me to use it all the time, I really want to. However, for now, I will give it a pass. Let me dig into it again in next few days.