Skip to main content
In this lesson, you will learn how routing works in Mizu. Routing controls what happens when someone visits a specific URL. You can define routes for different paths like /, /about, or /api, and Mizu will call the right handler function automatically. Each route uses a plain Go function that takes a *mizu.Ctx and returns an error. This keeps your code simple and easy to follow.

Code

Create a file named main.go and add this code:
package main

import (
	"log"
	"net/http"
	"time"

	"github.com/go-mizu/mizu"
)

// Home page handler
func home(c *mizu.Ctx) error {
	return c.Text(http.StatusOK, "Welcome to Mizu!")
}

// About page handler
func about(c *mizu.Ctx) error {
	return c.Text(http.StatusOK, "This is the About page.")
}

// Hello page handler with path parameter
func hello(c *mizu.Ctx) error {
	name := c.Param("name")
	return c.Text(http.StatusOK, "Hello, "+name)
}

// Time API handler returning JSON
func apiTime(c *mizu.Ctx) error {
	return c.JSON(http.StatusOK, map[string]string{
		"time": time.Now().Format(time.RFC3339),
	})
}

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

	// Register routes
	app.Get("/", home)
	app.Get("/about", about)
	app.Get("/hello/{name}", hello)
	app.Get("/api/time", apiTime)

	if err := app.Listen(":8080"); err != nil {
		log.Fatal(err)
	}
}

Run

Open your terminal and run:
go run .
Then open your browser and visit:

How it works

Each route is matched to a specific function. For example, /hello/{name} includes a placeholder, and Mizu automatically extracts the value so you can read it with c.Param("name"). When a request matches a path, Mizu runs the correct handler. Inside your function, you can use c.Text() to send text or c.JSON() to return structured data. Every handler returns an error so you can add custom error handling later if needed.

Try something new

Add a route that returns a custom status code or HTML content. For example:
func notFound(c *mizu.Ctx) error {
	return c.Text(http.StatusNotFound, "Page not found")
}
Register it like this:
app.Get("/404", notFound)
Then visit http://localhost:8080/404 to test it. Routing is the core of every Mizu app. Once you are comfortable defining routes and handlers, you are ready to build full pages and APIs. Next, continue to JSON Responses to learn how to send structured data from your routes.