Prerequisites
Before starting, make sure you have:- Go installed (version 1.22 or later)
- Mizu CLI installed
- A text editor
Step 1: Create the Project
Open your terminal and run:Step 2: Enter the Project
Step 3: Install Dependencies
go.mod with the correct version.
Step 4: Run the Server
Step 5: Test It
Open another terminal (keep the server running) and test with curl:http://localhost:8080 in your browser.
Step 6: Stop the Server
Go back to the terminal runningmizu dev and press Ctrl+C:
Understanding the Code
Now let’s look at the code. Openmain.go in your editor:
Key Concepts
1. Creating the Appapp.Getmeans “handle GET requests”"/"is the path (root URL)- The function is your handler - it runs when someone visits
/ cis the context - it has request info and response methodsc.Text(200, ...)sends a plain text response with status code 200
Exercise 1: Add a New Route
Let’s add an/about page.
Edit main.go:
Exercise 2: Return JSON
APIs usually return JSON. Let’s add a JSON endpoint. Editmain.go:
Exercise 3: URL Parameters
Let’s create a route that uses URL parameters. Editmain.go:
:name in the route is a parameter. Whatever value is in that position gets captured and is available via c.Param("name").
Exercise 4: Query Parameters
Query parameters are the?key=value part of URLs.
Edit main.go:
What You’ve Learned
In this tutorial, you learned:- Create a project with
mizu new - Run a server with
mizu dev - Add routes with
app.Get() - Return text with
c.Text() - Return JSON with
c.JSON() - Use URL parameters with
:nameandc.Param() - Use query parameters with
c.Query()