Skip to main content
In this tutorial, you’ll build a complete task management API with CRUD operations, validation, error handling, and authentication.

What We’ll Build

A REST API with:
  • List, create, read, update, delete tasks
  • Input validation
  • Error handling
  • Basic authentication
  • Request logging
Final API:

Prerequisites

  • Go 1.22 or later
  • Basic Go knowledge
  • A terminal and text editor

Step 1: Create the Project

Step 2: Create the Basic Server

Create main.go:
Run it:
Test:

Step 3: Define the Task Model

Add the Task struct above main():

Step 4: Create CRUD Handlers

Add these handler functions:
Update main() to register routes:
Add the missing import:

Step 5: Test the API

Step 6: Add Logging Middleware

Step 7: Add Error Handling

Create a custom error handler:
Update handlers to use custom errors:
Register the error handler:

Step 8: Add Authentication

Create an auth middleware:
Apply to protected routes:
Test with auth:

Complete Code

Here’s the final main.go:

What You Learned

  • Creating a Mizu application
  • Defining routes with path parameters
  • Reading and writing JSON
  • Using middleware for logging and auth
  • Custom error handling
  • Route grouping

Next Steps

Build a Website

Server-rendered pages with templates.

Add a Database

Connect to PostgreSQL or SQLite.