Rodrigo Oliveira
Rodrigo Oliveira

Reputation: 785

Golang unmarshal struct with error interface func

I'm trying to do Unmarshal from an struct that is complaint from Error Interface, but when i'm trying to get the Res structure Error content, it return the Error Interface string instead.

Example(with Error Interface complain): https://play.golang.org/p/ZZDTMSXYO7

Result: 2009/11/10 23:00:00 C: -> {Data:{Title:Lorem Ipsum data} Error:}

Example(Without Error Interface complain): https://play.golang.org/p/VmS0xIglC0

Expected: 2009/11/10 23:00:00 C: -> {Data:{Title:Lorem Ipsum data} Error:{Title:Lorem Ipsum error}}

Can anyone help us on this?

Upvotes: 0

Views: 661

Answers (1)

mkopriva
mkopriva

Reputation: 38233

log.Printf with the %+v formatting calls the Error method on a value that implements the error interface, which is the case with your Error type. So instead of printing the value's contents it prints the string that the Error method returns, which is an empty string in your example.

But that does not mean that the unmarshaler didn't unmarshal the json properly and you can see that by changing the formatting verb to %#v or by printing just the Title field.

https://play.golang.org/p/PgFCDnySb9

Upvotes: 3

Related Questions