News for hackers. Stuff that matters.
Re:Go: Ten years and climbing by christophilus on Thursday September 21, 2017 @09:39PM (#15308786)
I evaluated Go a few months ago for a web project, but it seemed as if there was a lot of manual work involved in mapping objects in and out of a MySQL database and in and out of JSON (basically, 50% of my day-to-day back end work). It just seemed too arduous.

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?

Re:Go: Ten years and climbing by ovao on Thursday September 21, 2017 @10:36PM (#15309038)
Go's HTTP server handles each request in its own goroutine, and any panics encountered are automatically recovered from. Nothing special needs to be done to prevent a panic from taking down the server.
Re:Go: Ten years and climbing by christophilus on Friday September 22, 2017 @03:43AM (#15310132)
Interesting. I had a panic take down my service,but I think I was trying fasthttp.
Re:Go: Ten years and climbing by agnivade on Friday September 22, 2017 @01:51AM (#15309699)
> What is the best way to map objects to/from JSON? - What is the best way to map objects to/from a database?

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.

Re:Go: Ten years and climbing by brownmenace on Thursday September 21, 2017 @10:57PM (#15309114)
Re:Go: Ten years and climbing by chimeracoder on Thursday September 21, 2017 @11:37PM (#15309233)
> - What is the best way to map objects to/from JSON? - What is the best way to map objects to/from a database?

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.

Re:Go: Ten years and climbing by pbohun on Thursday September 21, 2017 @10:04PM (#15308894)
Here's an example I wrote up for to/from json: https://gist.github.com/anonymous/8487446a2fd908910c66a2aafb...

Admittedly, mapping objects to/from database entries is a bit more complicated. There are several ORMs out there or you can role your own.

Re:Go: Ten years and climbing by rhinoceraptor on Thursday September 21, 2017 @10:24PM (#15308984)
I don't use go, but it doesn't really seem like CRUD apps are the sweet spot for it. When I think of go, I think of system software, that 10 years ago would have been written in C. I don't think of it as replacing Rails.
Re:Go: Ten years and climbing by christophilus on Thursday September 21, 2017 @10:20PM (#15308969)
Yeah. The repeating the field, only camelCased, in JSON annotations feels needlessly painful to me. Subjective, I know, but that kind of painfulness seems pervasive in my (brief) dabbling with Go, and is one of the main reasons I moved on.
Re:Go: Ten years and climbing by pbohun on Thursday September 21, 2017 @10:41PM (#15309059)
The json annotation is optional, but very powerful since it allows you to set the mapped name of the field to any name you like, allowing you to map json fields with snake_case, CamelCase, kebab-case, etc.
Re:Go: Ten years and climbing by ovao on Thursday September 21, 2017 @10:56PM (#15309112)
One alternative to writing struct tags by hand is to write a little generator code to map the field names from, say, a reference .json, then calling `go generate` as part of your build process.

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.

Re:Go: Ten years and climbing by GoodbyeEarl on Friday September 22, 2017 @01:10AM (#15309541)
This is also a turn down point for me, I wish there was better and easier JSON helpers.