You are setting an interface to a struct value, then passing Unmarshal a pointer to the interface. This is the wrong “order” of pointers and will result in Unmarshal overwriting the pointed to interface with the map. What you want to do is pass Unmarshal a pointer to your struct type:
var x X{} err := xml.Unmarshal(data, &x) or, equivalently via an interface: var x X{} var intf interface{} = &x err := xml.Unmarshal(data, intf) This boxing is exactly the same thing that happens automatically in the first function call. //jb On 16 Nov 2017, at 21:52, Diego Medina <fmpwiz...@gmail.com<mailto:fmpwiz...@gmail.com>> wrote: Hi, At work I have 3 different xml files I need to parse (different fields each), Because going from the xml bytes into a struct is pretty much the same for all 3 types, I thought I could do something like var payload interface{} if kind == "A" { paylaod = TypeA{} } if kind == "B" { paylaod = TypeB{} } if kind == "C" { paylaod = TypeC{} } .... xml.Unmarshal([]byte(file), &payload) but this fails. It doesn't give me an error, but Unmarshal doesn't recognize the payload variable as the right (new) type. I created a very simple snippet here https://play.golang.org/p/QvVbM--Zdt produces: gen is type: main.X //<== see how the type is correct here xml gen is {One:} json gen is map[One:uno] gen is type: main.X //<== here I declared the variable with the right type and Unmarshal works xml gen is {One:uno} json gen is {One:uno} Hope this is enough information. Thanks Diego -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com<mailto:golang-nuts+unsubscr...@googlegroups.com>. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.