Fiber URL Params: Colon vs Asterisk
In this tutorial, you'll learn about Fiber URL Params: Colon vs Asterisk. We cover key concepts, practical examples, and best practices.
Route parameters in Fiber -- Use colon-prefixed :params for path segments and asterisk *params for catch-all routes.
The Problem
Fiber uses Express.js-style routing. :param matches a single path segment, while *param matches everything including slashes.
Wrong
app.Get("/users/:id", func(c *fiber.Ctx) error {
return c.SendString("User: " + c.Params("id"))
})
Output:
$ curl http://localhost:8080/users/42
User: 42
$ curl http://localhost:8080/users/42/profile
// 404
Right
app.Get("/users/:id", func(c *fiber.Ctx) error {
return c.SendString("User: " + c.Params("id"))
})
app.Get("/users/:id/*file", func(c *fiber.Ctx) error {
return c.SendString("User: " + c.Params("id") + ", File: " + c.Params("file"))
})
Output:
$ curl http://localhost:8080/users/42/profile
User: 42, File: profile
Prevention
- Use :param for single segment matching
- Use *param for catch-all matching (including slashes)
- Route order matters: more specific routes first
- Use c.Params("name") to extract both types
- Use query params for optional data
Common Mistakes with fiber params
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world GO code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. DodaTech tutorials help Go developers build production-ready software used by millions.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro