mercredi 14 décembre 2016

Unmarshall nested interface{} into user supplied struct pointer

I've got a Set function that wraps a users object (or variable) in my own struct called sessions. It assigns it to the Value field of my sessions struct. The Set function then marshalls this struct and assigns the string somewhere in storage.

My problem is that I'm not sure how to implement my Get function to only return the unmarshalled struct stored in the Value field, opposed to the entire sessions wrapper struct.

I've made a very simple example demo'ing what I'm talking about:

http://ift.tt/2gNxbGV

I can't use a type assertion in the assignment in my Get func because I don't know what type the user is going to use in advance.

I suspect there may be a way using reflection to accomplish this?

Alternatively, I guess I could return a interface{} instead of trying to assign to a pointer, and then force the user to do the type assert themselves? Would be cool if I could hide all that from the user though.





Aucun commentaire:

Enregistrer un commentaire