Menu

Attribute Routing Comes to Go's ServeMux, in Struct Tags
📰

Attribute Routing Comes to Go's ServeMux, in Struct Tags

DEV Community: go·Andy Cross·7 days ago
#K4MdJoQs
#devtogo#devto#dev#orders#mapget
Long article (3352 words) — no AI summary yet.
Reading 0:00
15s threshold

The HTTP services post in this series gave routing a section called "The Weak Spot", showed four lines of the Go 1.22 mux, and moved on to chi, gorilla and gin. This post covers what that section left out: the pattern language set against attribute routing, and the things ASP.NET Core does around its router that Go's mux does not attempt. It then fills most of that list with a small library I wrote for the purpose, mapget . Every snippet and every response below was run on go1.27.3.

What you had in C

[ ApiController ] [ Route ( "orders" ) ] public class OrdersController : ControllerBase { [ HttpGet ( "{id:int}" ) ] public ActionResult < Order > Get ( int id , [ FromQuery ] string [ ] expand ) { . . . }

[ HttpPost ] public ActionResult < Order > Create ( NewOrder body ) { . . . return CreatedAtAction ( nameof ( Get ) , new { id = order . Id } , order ) ; } }

Enter fullscreen mode

Exit fullscreen mode

That controller uses six separate features. The route template is matched.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.