Get the latest tech news
Decoding JSON sum types in Go without panicking
The Go programming language doesn't have native support for sum types, but we'll see how we can emulate them, how to decode and encode them into JSON, and how in some cases they can help avoid runtime panic exceptions.
The Go programming language doesn't have native support for sum types, but we'll see how we can emulate them, how to decode and encode them into JSON, and how in some cases they can help avoid runtime panic exceptions. Not only do these action-specific structs provide more type-safety, but if I forget to handle a variant in my switch statement (or if I add a new one that implements the sealed interface), the go-check-sumtype linter will catch it instead of getting an error at runtime! And the Action sum type decodes from/encodes to JSON right out-of-the box (with the caveat that, at the time of writing, it uses the adjacently tagged representation with no way of configuring it).
Or read this on Hacker News