If you are doing heavy sql queries, then it might be beneficial for you to use an ORM. There are plently of those out there, so choose one you like. Just create structs for your tables and run your queries.
If you are doing light/moderate sql. Just have an interface for your db calls, wrap your responses in a struct and use json tags for json encoding/decoding.
> What is the best way to prevent a request from crashing the server and killing a bunch of unrelated requests?
I use negroni. And its recovery middleware does exactly that.
I'd recommend using the JSON to generate your struct definitions, in a schema-like fashion. I wrote a tool to automate that process: https://github.com/ChimeraCoder/gojson
I use this as a step in the build system for the projects, which ensures that the definitions stay in sync with the API clients/servers themselves.
Admittedly, mapping objects to/from database entries is a bit more complicated. There are several ORMs out there or you can role your own.
I've wondered whether a wrapper library could do rule-based field name mapping automatically with Go's reflection facilities (which could be a productivity win), but I haven't really looked at it closely.
The second thing that bothered me was that an edge-case bug in one of my HTTP handlers paniced the app. If that had happened in production while under load, many users would have gotten an error due to a bug in one user's request.
So, serious questions:
- What is the best way to map objects to/from JSON? - What is the best way to map objects to/from a database? - What is the best way to prevent a request from crashing the server and killing a bunch of unrelated requests?