From Bland to Spiky: How Generics Made My Service Super Robust
summary
Built a REST service using Go generics across API, service and GORM database layers to replace a dozen near-identical CRUD endpoints, avoiding switch payload.(type) and code generation. Uses Go generics (post‑1.18), fuo for OpenAPI, UUIDs for IDs, and saves items for address/name/dateOfBirth models into separate tables; implemented generic factory pattern with required getBase/setBase helpers and lightweight casting. Added toModel/toResponse request/response wrappers for frontend shape changes. The system works in production, mobile clients are satisfied, and compile/runtime overhead is negligible.
AI-written summary · about
chapters (17)
- 0:00Introduction and speaker intro
- 1:30Problem with legacy API
- 4:02Service design goals
- 7:16Why generics were needed
- 9:24Failed template approach
- 11:33Generics 101 crash course
- 14:25First generic implementation
- 17:00Interface and base model
- 19:37Third iteration with constraints
- 22:09Database layer integration
- 23:36Testing and validation
- 24:10Hedgehog factory success
- 25:58Mobile app requirements change
- 28:34Fourth iteration for responses
- 31:09Final working solution
- 33:00Lessons learned and benefits
- 36:56Q&A session
related talks
16mGo Generics
21mBuilding APIs in Go The Stack Without a Framework
23mRefactor Cloud applications in Go 1.18 with generics
31mImplementing RESTful APIs
37mPatterns for Scalable Web Services in Go
28mWhat's Behind Generics in Go
23mGenerate OpenAPI Schema from Golang Code with the Fuego Framework.
29mGenerics Unconstrained!
