Courier of the day

Exercise 2 of 4 in chapter 1 · 100 XP · 2 objectives checked by tests

Topics covered: Route parameterVariableString

Every morning, each courier opens the app, which greets them by their first name. Margot wants the API to do it.

Your mission

  1. In routes/web.go, add a GET /couriers/{prenom} route (prenom is French for “first name”).
  2. It reads the prenom parameter from the URL.
  3. It responds with the JSON {"coursier": "Margot", "message": "Have a good round, Margot!"}, with the first name from the URL in both keys.

Reminder

As in Laravel, a route parameter is declared between curly braces. Goravel doesn't pass it as an argument to the function: you ask the request for it, ctx.Request().Route("prenom"), which returns a string.

In Go, := declares a variable and gives it its value in one go; its type is inferred from the value. prenom := ctx.Request().Route("prenom") makes prenom a string. Once it's declared, you change it with = alone. And the Go compiler refuses a declared variable that is never used (the browser's interpreter is more forgiving, a real project is not).