Building a Simple Web Application with GoFr

Soumyadip Sarkar
4 min readOct 31, 2024

Go (Golang) has gained immense popularity for its simplicity and performance, especially in the realm of web development. Recently, I stumbled upon GoFr, a minimalist web framework for Go, and decided to give it a try. In this article, I’ll walk you through building a simple web application using GoFr, highlighting how straightforward and efficient it is.

Introduction to GoFr

GoFr is a lightweight web framework designed to make building web applications in Go as effortless as possible. It embraces Go’s philosophy of simplicity and performance, providing just enough structure without getting in your way.

Why GoFr?

  • Minimalistic: Focuses on what matters without unnecessary bloat.
  • Performance-Oriented: Built on Go’s efficient concurrency model.
  • Easy to Learn: If you’re familiar with Go, picking up GoFr is a breeze.
  • Flexible: Allows you to structure your application as you see fit.

Setting Up the Project

Let’s dive into building a basic web application that serves two routes: / and /about.

Prerequisites

  • Go (Golang): Ensure you have Go installed version 1.21 or above. Download Go.
  • Git: For version control (optional but recommended). Download Git.

Project Structure

Our project will have the following structure:

gofr-app/
├── main.go
├── controllers/
│ └── controllers.go
└── routes/
└── routes.go

Creating Your First GoFr Project

Step 1: Initialize Your Project

Create a new directory for your project:

mkdir gofr-app
cd gofr-app

Step 2: Initialize the Module

Ensure you’re in the gofr-app directory and run:

go mod init github.com/yourusername/gofr-app

Replace yourusername with your GitHub username or any other module path you'd like to use.

Step 3: Download Dependencies

Run:

go get -u gofr.dev/pkg/gofr

This command downloads the necessary dependencies, including GoFr.

Writing the Code

1. main.go

This is the entry point of our application.

package main

import (
"github.com/neuralsorcerer/gofr-app/routes"
"gofr.dev/pkg/gofr"
)

func main() {
app := gofr.New()

routes.Register(app)

app.Run()
}

Explanation:

Import Statements:

  • github.com/neuralsorcerer/gofr-app/routes: Imports our route definitions.
  • gofr.dev/pkg/gofr: Imports the GoFr framework.

Main Function:

  • app := gofr.New(): Initializes a new GoFr application.
  • routes.Register(app): Registers our routes with the application.
  • app.Run(): Starts the application on the default port (usually 8000).

2. routes/routes.go

This file defines the routes for our application.

package routes

import (
"github.com/neuralsorcerer/gofr-app/controllers"
"gofr.dev/pkg/gofr"
)

func Register(app *gofr.App) {
app.GET("/", controllers.Home)
app.GET("/about", controllers.About)
}

Explanation:

Import Statements:

  • github.com/neuralsorcerer/gofr-app/controllers: Imports our controller functions.
  • gofr.dev/pkg/gofr: Imports the GoFr framework.

Register Function:

  • Associates HTTP GET requests to the root / and /about paths with the Home and About controller functions, respectively.

3. controllers/controllers.go

This file contains the logic for handling requests.

package controllers

import (
"gofr.dev/pkg/gofr"
)

func Home(c *gofr.Context) (interface{}, error) {
return "Welcome to the Home Page!", nil
}

func About(c *gofr.Context) (interface{}, error) {
return "About GoFr Application", nil
}

Explanation:

Import Statements:

  • gofr.dev/pkg/gofr: Imports the GoFr framework to access the Context.

Controller Functions:

Home Function:

  • Handles requests to /.
  • Returns a welcome message.

About Function:

  • Handles requests to /about.
  • Returns an about message.

Both functions return an interface{} (the response body) and an error. Since we're not handling any errors here, we return nil for the error.

Running the Application

Now that we have our files set up, let’s run the application.

Step 1: Start the Application

Run:

go run main.go

You should see output similar to:

Server started on port 8000

Step 2: Test the Endpoints

Open your web browser and navigate to:

Home Page: http://localhost:8000/

  • You should see “Welcome to the Home Page!”

About Page: http://localhost:8000/about

  • You should see “About GoFr Application”

Understanding How It Works

GoFr Application Initialization

In main.go, we create a new GoFr application instance with gofr.New(). This instance, app, is our main application that will handle incoming HTTP requests.

Registering Routes

We separate our route definitions into routes/routes.go for better organization. The Register function associates URL paths with controller functions.

Handling Requests with Controllers

Our controllers in controllers/controllers.go are simple functions that return a response. The gofr.Context parameter provides access to request data, though we don't use it in this basic example.

Advantages of This Structure

  • Modularity: Separating concerns (main app, routes, controllers) makes the codebase easier to manage and scale.
  • Readability: Clear structure helps new developers understand the flow quickly.
  • Maintainability: Changes in one part of the application are less likely to affect others.

Next Steps

Now that we have a basic application running, here are some ideas to extend it:

  • Dynamic Content: Use the Context to read query parameters or request bodies.
  • Templates: Integrate HTML templates for rendering dynamic web pages.
  • Database Integration: Connect to a database using GORM or another ORM to handle data persistence.
  • Middleware: Implement middleware functions for logging, authentication, etc.
  • API Development: Build a RESTful API by adding more routes and handlers.

Conclusion

GoFr offers a clean and straightforward way to build web applications in Go. By leveraging Go’s strengths and providing a minimalist framework, it allows developers to focus on writing meaningful code without getting bogged down by complexity.

Why I Like GoFr:

  • Simplicity: It doesn’t try to do everything, which keeps things simple.
  • Flexibility: You’re not locked into a specific way of doing things.
  • Performance: Go’s efficiency shines through with GoFr.

Thank you for reading! If you found this article helpful, please give it a clap 👏 and share it with others who might be interested.

Happy coding with GoFr!

--

--

Soumyadip Sarkar
Soumyadip Sarkar

Written by Soumyadip Sarkar

Independent Researcher & Software Engineer

No responses yet