4 ms·
Here's an example I wrote up for to/from json: https://gist.github.com/anonymous/8487446a2fd908910c66a2aafbe7b1ef https://gist.github.com/anonymous/8487446a2fd9
by pbohun 9y ago
Here's an example I wrote up for to/from json:
https://gist.github.com/anonymous/8487446a2fd908910c66a2aafbe7b1ef 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.
- christophilus 9y agoYeah. 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.
- pbohun 9y agoThe 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.
- ovao 9y agoOne 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.
- rhinoceraptor 9y agoI 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.