July 2019
Intermediate to advanced
458 pages
12h 12m
English
When working with interface types, the encoding part is really straightforward because the application knows which data structure is stored within the interface and will proceed with the marshaling. Doing the opposite operation is not quite as straightforward because the application received has an interface rather than a data structure and does not know what to do and, therefore, ends up doing nothing.
A strategy that works really well (even if it involves a little boilerplate) is using a concrete type container, which will permit us to handle the interface in the UnmarshalJSON method. Let's create a quick example by defining an interface and some different implementations:
type Fooer interface { Foo()}type A struct{ Field string ...Read now
Unlock full access