DEV Community

luthfisauqi17
luthfisauqi17

Posted on

Getting Started with Golang Chi: A Guide to Building a Simple API

Are you looking for a lightweight and efficient router to build HTTP services in Go? If so, Chi is the answer. In this guide, we will walk through the basics of using Chi to build a simple yet powerful Golang application.

What is Chi?

According to its official documentation, Chi is a lightweight, idiomatic, and composable router for building HTTP services in Go. Its key advantages include:

  • Lightweight: Extremely fast and minimal.
  • Robust: Built for production-grade reliability.
  • No External Dependencies: Keeps your project clean and manageable.

Before diving in, it is highly recommended to read the official Chi documentation to understand its core features.


Setting Up Your Project

1. Initialize the Project

The first step is to create your workspace and initialize a Go module. Open your terminal and run:

go mod init example.com/golang-simple
Enter fullscreen mode Exit fullscreen mode

2. Install the Chi Library

Next, add the Chi dependency to your project. Ensure you are using the latest version (v5) to access the best features:

go get -u github.com/go-chi/chi/v5
Enter fullscreen mode Exit fullscreen mode

Writing the Core Code

Open your code editor (such as Visual Studio Code) and create a file named main.go. Here is the basic structure to get your server up and running:

package main

import (
    "net/http"
    "github.com/go-chi/chi/v5"
    "github.com/go-chi/chi/v5/middleware"
)

func main() {
    // 1. Initialize the Chi router
    r := chi.NewRouter()

    // 2. Add Middleware (optional but recommended)
    r.Use(middleware.Logger)

    // 3. Create a Dummy API (GET Endpoint)
    r.Get("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello World"))
    })

    // 4. Run the Server on Port 3000
    http.ListenAndServe(":3000", r)
}
Enter fullscreen mode Exit fullscreen mode

Code Breakdown:

  • chi.NewRouter(): Creates a new instance of the Chi router.
  • middleware.Logger: A built-in Chi middleware that logs every incoming request to your terminal.
  • r.Get: Defines an HTTP GET route on the root path (/).
  • http.ListenAndServe: Opens port 3000 to listen for incoming requests.

Running the Application

To see your API in action, run the following command in your terminal:

go run main.go
Enter fullscreen mode Exit fullscreen mode

Once the server is running, open your browser and go to localhost:3000. You should see the message "Hello World" on the screen. Additionally, check your terminal to see the logs generated by the logger middleware.


Conclusion

Building an API with Golang Chi is quick and straightforward. Its simple structure allows developers to create clean web services without the overhead of heavy libraries. This is just the beginning—you can explore more complex routing and custom middleware as you grow!


Want to see the visual tutorial? Watch the full video on the luthfisauqi17 YouTube channel.

Top comments (0)