Hi Jakob, Thanks for your reply, maybe my original email didn't include enough information. What you described is what I ended up doing as a work around in my code:
if kind == "a" { var ret trading.TypeA if err := ParseRefData(f, &ret); err != nil { log.Fatalf("failed to decode file: %s from zip: %s, error: %s", row.Name, path, err) } prepareFeedForMongo(acmsess, stats, ret) } if kind == "b" { var ret trading.TypeB if err := ParseRefData(f, &ret); err != nil { log.Fatalf("failed to decode file: %s from zip: %s, error: %s", row.Name, path, err) } prepareFeedForMongo(acmsess, stats, ret) } if kind == "c" { var ret trading.TypeB if err := ParseRefData(f, &ret); err != nil { log.Fatalf("failed to decode file: %s from zip: %s, error: %s", row.Name, path, err) } prepareFeedForMongo(acmsess, stats, ret) } See how I'm calling ParseRefData and prepareFeedForMongo 3 times and handling errors in 3 diff places, I'd like to handle the errors on one place, and call ParseRefData and prepareFeedForMongo only once, all I'd like to do inside each if statement is "change" the type of ret to the expected type. Unless this isn't possible, but I wonder why Printf %T shows me the type I was expecting. Thanks Diego On Thu, Nov 16, 2017 at 4:13 PM, Jakob Borg <ja...@kastelo.net> wrote: > 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> 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. > For more options, visit https://groups.google.com/d/optout. > > > -- Diego Medina Go Consultant di...@fmpwizard.com http://blog.fmpwizard.com/ -- 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.