DEV Community

luthfisauqi17
luthfisauqi17

Posted on

Mastering Routing in Golang with Chi: A Beginner’s Guide

In modern web development, understanding how to manage your application's endpoints is crucial. If you are building HTTP services in Go, you need a router that is both lightweight and powerful. That’s where Chi comes in.

In this post, we will explore the fundamentals of routing using the Chi library, building upon our previous basic implementation.

What is Routing?

At its core, routing refers to how an application’s endpoints respond to specific client requests. Think of a router as a traffic controller: it takes an incoming request (like a URL) and directs it to the appropriate function to handle it.

Getting Started with Chi Router

If you followed the previous tutorial, you might have noticed we already initialized a router. In Chi, creating a new router is as simple as:

r := chi.NewRouter()
Enter fullscreen mode Exit fullscreen mode

This r object is now responsible for managing all the endpoints in your application.

1. Defining Endpoints

An endpoint is a specific path that users can hit to get a response. For example, a simple "Hello World" endpoint looks like this:

r.Get("/", func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello World"))
})
Enter fullscreen mode Exit fullscreen mode

This code tells the server: "If someone hits the root (/) path using a GET request, return the string 'Hello World'."

2. Refactoring for Cleaner Code

While using anonymous (inline) functions is quick, it can make your main function look cluttered as your app grows. A better practice is to separate your logic into dedicated handler functions.

Before (Inline):

r.Get("/", func(w http.ResponseWriter, r *http.Request) { ... })
Enter fullscreen mode Exit fullscreen mode

After (Clean):

func handleHelloWorld(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello World"))
}

// Inside your main function:
r.Get("/", handleHelloWorld)
Enter fullscreen mode Exit fullscreen mode

This approach makes your code much more readable and easier to maintain.

3. Adding Multiple Endpoints

A single router can store and manage multiple endpoints. You can easily add more routes to handle different parts of your application. For instance, let’s add a /test endpoint:

func handleTest(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("This is a test response"))
}

// Registering the new route
r.Get("/test", handleTest)
Enter fullscreen mode Exit fullscreen mode

Now, if you navigate to localhost:8080/ you get "Hello World", and if you go to localhost:8080/test, you receive the test handling response.


Conclusion

Routing is the backbone of any web API. By using Chi, you get a clean, idiomatic way to handle complex paths without the overhead of heavy frameworks.

Key Takeaways:

  • A Router manages your application's endpoints.
  • Endpoints define how the server responds to specific paths.
  • Separating handlers into their own functions keeps your codebase professional and clean.

Stay tuned for the next topic where we will dive deeper into more advanced Chi features!


Helpful Resources:

Top comments (0)