geddit
geddit copied to clipboard
Reddit Comments
We're currently using a slapped together implementation to fetch comments from reddit.com. I'd like to completely model the whole thing out using static types and just encoding/json.Unmarshal() the whole thing. A lot of the complexity around the type hierarchy needed to parse the JSON can be hidden by struct embedding.
However, the "replies" field on comments either returns a list of objects or empty string if there are none. Sadly, I think this will require some kind of run-time type assertion.
Is it not possible to make use of omitempty?
Nope, I tried here. The problem is that the empty "replies" should be an []
not a ""
. It's basically a mismatch between a loosely-typed language (JavaScript) and a strongly typed language (Go).
You just need to type alias []Comment and then implement the Unmarshaller interface. See example here: https://play.golang.org/p/Gs9DxuKqBx
Yep. That'd definitely work.