Richard Bucker

Dynamic Un/Marshalling in Go

Posted at — Nov 12, 2013

I cannot say for certain that the following applies to all of Go’s encoding schemes as I can see at least one use-case in XML that would fail; and upon reflection (no to be confused with the library) this might actually create more problems than it solves if it were not for the power of Go’s reflect lib.The problem:Given a message, in string¬†or []byte¬†format, UnMarshal that message to it’s internal structure; however the message can be one of any number of specific messages. As an example consider a message queue where the messages represent arbitrary events.The Solution:(a)Implement a wrapper structure that encapsulates the target classes and (b)when UnMarshal’ing add the wrapper to the outside.