[Crazy Go Day] Why Using Gin For Golang Backend?
Overview
Hi there !! Recently I am preparing for the backend internship of Dcard, and this series of articles (Crazy Go Day) are some development skills I learned during this period of time :)
Last article we discuss about how to improve access efficiency by caching, and I will continue to explain my backend implementation today !
About Gin-Gonic framework
Briefly speaking, Gin is a light weight http web framework aims for high performance. According to introduction on official website, it features a martini-like API with performance that is up to 40 times faster thanks to httprouter, which is a native golang library.
Why I choose Gin instead of implementing http server by myself?
- I have no time to implement goroutines. Gin already implemented awesome multi-goroutine schedule which serves each request by an individual goroutine. The reason of doing this is to avoid request queuing, so that the performance will be better.
- Gin implement the interface of
context.Context
, so I can easily control flows and pass value within it. Moreover, when we are using native library, we need to usechild context
to control some inner services. For instance, process like database querying sometimes take longer time than we expected initially, so we will pass acontext.WithTimeOut()
into it. However, if we are using Gin, we can just simply passgin.Context.Request.Context()
into the database querying service, and Gin will handle request for us :) - Gin has fantastic middleware technique which make scaling a simple job.
Example: controller/url.go
Let’s take a look at my request controller:
I will explain my error handling method later. As we can see above, just pass c.Request.Context()
into the service postgreSQLClient.QueryRowContext()
and everything about context control is done ! Moreover, as I mentioned above, this controller will handle the incoming request with individual goroutine!
How I do error handling in Gin
Error handling is an important part of backend development, and both frontend engineer and backend want a elegant error return.
When I am thinking about how to do error handling on this project, I aim for the following purposes:
- Uniform return format
- Error message in detail
- Reasonable status code
Base on these, I design the following return object format:
StatusCode: <Offcial http status code>
ErrorCode: <Error title>
Message: <Clear error message in detail>
Errorcode acts like a errorType
here, which responsible for error classification. I know that StatusCode already classify many kinds of error, but it is some how not clear enough…
For example, we have many kinds of InternalServerError
which are cause by various kinds of services (database insertion, connection stuff, external API calling… etc,). When there is thousands of lines of code, if there are more than one error happened at the same time, it’s hard for us to quickly classify them and debug.
With ErrorCode
and Message
in server log, we can not only find the type of error quickly, but also know the reason why it occurred. Sometimes the error does not related to your code but related to the library you import. By reading the message, you can find out the problem without googling.
Code Example: (And I also classify by StatusCode, make the code more readable and friendly for the next upcoming developer !)
When there is error happened, we pass these constant object by pointer !
(p.s. Some error has message initially while others don’t, since some error is logic error
, which is caused by programmer himself .)
Take a look at following error happening example:
Thanks to elegant error handling technique provided by Golang itself, every time when err != nil
, I will copy the corresponding error constants and return the status code and the content :)
Congrats guys !!! We now have basic understanding of Gin and also have reasonable error handling technique :)
Source code here: src ❤️
Thank you for your time reading. Any suggestions are welcomed and feel free to point me out if anything is unclear.
See u guys next time! (Happy coding~