How to get a pointer to a variable that's masked as an interface?
<p>I prefer not to dive into the rationale of the situation below. It has to do with unmarshaling an serialized object that can be any of a fixed set of types, but you don't know which type.</p>

<p>I have the following types:
</p>

<pre class="lang-go prettyprint-override"><code>type I interface {
    Do()
}

type someI struct {}
func (i *someI) Do() {}

type otherI struct {}
func (i *otherI) Do() {}
</code></pre>

<p>So, two structs <strong>of which the pointers implement interface <code>I</code></strong>.</p>

<p>Now I have this method that wants to return a value of type <code>I</code>:</p>

<pre class="lang-go prettyprint-override"><code>func GetSomeI(marshalled []byte) (I, error) {
    var obj interface{}
    // The following method magically puts an instance
    // of either someI or otherI into obj.
    magicUnmarshall(marshalled, obj)
    // The problem now is that we cannot return obj,
    // because the raw structs don't implement I.

    // One solution would be to do a type switch like this:
    switch obj.(type) {
    case someI:
        i := obj.(someI)
        return &amp;i, nil
    case otherI:
        i := obj.(otherI)
        return &amp;i, nil
    default:
        return nil, errors.New("marschalled object was not of type I")
    }

    // But now consider the case that there are quite some
    // different implementations of I.
    // We would prefer to have a general way of getting
    // a reference to obj.
}
</code></pre>