title: Go deserialization when type is not known
body: <p>I'm writing a package in go to send messages between services, using a specific type of transport.</p>
  
  <p>I'd like the package to not understand the type of messages being sent.
  My first thought is to serialize the message object into json, send that, deserialize on the receiving end, and pass the go object (as an <code>interface{}</code>) to the subscribing code.</p>
  
  <p>The serialization isn't a problem, but I don't see how the generic package code can deserialize the message since it doesn't know the type. I thought of using reflect <code>TypeOf()</code>, and passing that value as part of the message. But I don't see how to accomplish this since Type is an interface and the implementing rtype is not exported.</p>
  
  <p>If the receiving app gets an <code>interface{}</code>, it is going to have to check the type anyways, so maybe it should just do the deserialization. Or the receiver could provide a reflect Type so the package can deserialize?</p>
  
  <p>Or it could give the receiver a map of field name to value, but I'd prefer the actual type.</p>
  
  <p>Any suggestions?</p>
  
  <p>Let me add an example:</p>
  
  <p>I have a go channel for sending change notifications of different types of objects. Since go doesn't support tagged unions, I define the channel type as:</p>
  
  <pre><code>type UpdateInfo struct {
      UpdateType UpdateType
      OldObject interface{}
      NewObject interface{}
  }
  </code></pre>
  
  <p>The receiving end of the channel gets an UpdateInfo with OldObject and NewObject as the actual concrete object types that were sent.</p>
  
  <p>I want to extend this to work between applications, where the transport will be via a message queue to support pub/sub, multiple consumers, etc.</p>