Why Use GoFr for Golang Backend?

# Why use GoFr for Golang Backend?

## GoFr is an opinionated Go Framework for accelerated microservice development, according to its official website, it features and promises battle tested code on actual production environment, it has…

![Image](https://miro.medium.com/v2/resize:fit:1200/1*6F07WixV6miJ-hZTVHgYIQ.png)

**About GoFr framework**

GoFr is an opinionated Go Framework for accelerated microservice development, according to its official [website](https://gofr.dev/), it features and promises battle tested code on actual production environment, it has built in observability tools such as health-check and heartbeat URLs which suffice Kubernetes liveness and readiness probe, tracing, metrics, and structured logs with different level to monitor your application.

It also has multiple different data sources support such as mysql, postgres, kafka, google, mqtt etc.

By all means it makes sure, you just need to focus on your business’s core logic, while it makes sure, your applications are production ready from the time of POC.

**Why did I choose GoFr instead of implementing http server by myself or using any other framework?**

It removed my worries about making my application consistent and allowing me to observe my application while I can focus on my business logic.

1. **REST API Principles**: GoFr ensures adherence to REST principles with its structured framework, simplifying API design.
2. **Logging**: Built-in logging features facilitate easy monitoring of incoming requests.
3. **Metrics and Tracing**: GoFr supports effortless integration of metrics and tracing, aiding performance monitoring and request tracing.
4. **CORS Configuration**: Easily configure Cross-Origin Resource Sharing (CORS) policies through GoFr’s built-in options.
5. **Dynamic Log Level**: Change log levels remotely without application restarts, enhancing operational flexibility.
6. **Kubernetes Compatibility**: Built-in health check and heartbeat endpoints support Kubernetes deployments.
7. **Database Integration**: Native support for popular databases simplifies database connection management.
8. **IoT Integration**: Native MQTT support and default connection to HiveMQ enable seamless IoT integration.

Apart from that, the best one among all of these is the way it takes very less lines of code to register a route, while it is doing a lot of other things.

These features are something which are not available in any of the popular frameworks like Gin, Echo, Beego, Fiber.

**Sample Route Registration**

```
package main  
  
import "gofr.dev/pkg/gofr"  
  
func main() {  
 // Create a new application  
 a := gofr.New()  
  
 // Add the routes  
 a.GET("/hello", func (c \*gofr.Context) (interface{}, error) {  
 return “Hello World”, nil  
})  
  
 // Run the application  
 a.Run()  
}
```

Just with three lines and a single primary dependency our Hello World application is production ready.

![](https://miro.medium.com/v2/resize:fit:640/format:webp/1*cMTtmpghD08GcV8bmk8rAQ.png)

Logs on app startup with app and metrics port

We get the following well formatted response when we hit our `/hello` endpoint.

![](https://miro.medium.com/v2/resize:fit:640/format:webp/1*UCnLz6vpIKg5cBb5uk9qkw.png)

Response from GoFr server

**Kubernetes liveness and readiness probe**

GoFr by default create routes for liveness and readiness probe where in on `/.well-known/health`endpoint it checks the status of all datasources, services connected to the current service.

![](https://miro.medium.com/v2/resize:fit:640/format:webp/1*qSZsDaTZK_7IbEk1xFLGpw.png)

Health Check Response

Where, for `/.well-known/alive` endpoint it checks if the current service is accessible

![](https://miro.medium.com/v2/resize:fit:640/format:webp/1*Lr4EeRf8MNxGFjYXFimj3g.png)

Aliveness Response

**Tracing with GoFr**

By adding the config \`TRACE\_EXPORTER=gofr\` in the config file `.env`.
Tracing was enabled and traces were being exported to [tracer.gofr.dev](https://tracer.gofr.dev/).

![](https://miro.medium.com/v2/resize:fit:640/format:webp/1*sTVDpn2nGXq4nec4b5Q0gw.png)

Tracing on accessing /hello endpoint

And we see the traces for our service, a more complicated sample of this where we can see how our request flows when multiple different endpoints are hit within a same request.

![](https://miro.medium.com/v2/resize:fit:640/format:webp/1*8yOCrYAFtX7QA185r6LV7A.png)

Trace with Redis

**Visualising Metrics**

In every GoFr application metrics are exported on port `2121`at `/metrics` endpoint, which can be visualized on grafana using [GoFr’s Grafana](https://grafana.com/grafana/dashboards/19905-service-monitoring-dashboard-all-services/) Dashboard.

Dashboard with simulated production system.

![](https://miro.medium.com/v2/resize:fit:640/format:webp/1*5aoORDYF4HLrqv9vnkIE5g.png)

System Information with no of Go-routines and memory utilization of application.

![](https://miro.medium.com/v2/resize:fit:640/format:webp/1*KVoB2NbAc04dYZDpySSXNg.png)

Request Response time along with Latency Distribution Graph

![](https://miro.medium.com/v2/resize:fit:640/format:webp/1*X2AUTATS44Alxtn1W_7SAA.png)

Request Count over time and different status codes returned

It also contains metrics for different data sources connected to the application.

![](https://miro.medium.com/v2/resize:fit:640/format:webp/1*XsND4HKZrqnXK9C1ac8uzw.png)

Response time and latency for outbound requests

![](https://miro.medium.com/v2/resize:fit:640/format:webp/1*DEeK8isn0dHdEP5E4qww1A.png)

Redis query count along with count based on different commands

Similar, graphs are available for different PubSub systems, SQL databases etc.

**Errors in GoFr**

We just need to return the error received, and GoFr by default formats the error in the following format.
An error returned from MySQL is being depicted in the following example:

```
{  
    "error": {  
        "message": "Error 1146 (42S02): Table 'test\_db.EMPLOYEE' doesn't exist"  
    }  
}
```

**Conclusion**

While writing code using gofr, I didn’t worry about minor things and focussed on my main business logic which not only increased my productivity, but I was able to monitor my application, which was not possible by using any other framework, or relying on default golang packages.

Moreover, I didn’t compromise on performance as from the following article [GoFr vs Echo: A Performance and Ease of Use Comparison](https://medium.com/@mundhraumang.02/gofr-vs-echo-a-performance-and-ease-of-use-comparison-47cff51cac6e).
GoFr surpassed Echo’s performance if I had to setup tracing metrics logging and other features myself.

Support GoFr by giving a ⭐star: [https://github.com/gofr-dev/gofr](https://github.com/gofr-dev/gofr)