On Tue, 28 Jan 2014 06:24:53 -0800
Ian Lance Taylor <
ia...@golang.org> wrote:
> > Hi, as I understand Go presumes strict error handling. So why there
> > is no way to way to check if json was unmarshaled correctly to
> > struct with values (not pointers)?
> > Assume we have a struct
> >
> > type My struct {
> > MissingField string
> > ExistingField string
> > }
> >
> > and json "{"existingfield" : "somevalue"}".
> >
> > The values of My will be My{MissingField:"",
> > ExistingField:"somevalue"} and no error at all will be returned. I
> > had no MissingField in json but nevertheless the unmarshaling was
> > done correctly. You can make it in the following way:
[...]
> It's a design choice. There is no need to have your Go struct exactly
> match your JSON format. Since you can detect which fields were
> present by using pointers, you can write your code to work in whatever
> way seems best for your application.
Having the json package process some explicit tag on struct members,
like, say, `json:must`, would do the trick for the OP.
Not that I need it personally, but I think I understand their wish to
not use indirection through pointers and still be able to distinguish
between an empty string and a missing value.