Why not provide a "NewFoo() *Foo" function
instead? https://play.golang.org/p/GE8cMgwe24
On Monday, March 27, 2017 at 10:42:46 AM UTC-6, traetox wrote:
>
> Hello all,
>
> I have swept through the list and gone through the pkg/encoding/json
> package and _believe_ I haven't missed anything, but am not 100% sure.
>
> When encoding structures into JSON which contain slices/arrays/maps my
> frontend devs consistently complain about the encoding of empty or nil
> members which they have to do a little extra work to deal with.
>
> For example, structure is defined as:
>
> type Foo Struct {
> Bar []string
> Baz map[string]int
> }
>
> if any of the members of the struct (Bar, Baz) are not initialized, or
> empty the encoding is:
>
> {
> "Bar": null,
> "Baz": null,
> }
>
> What they want is :
>
> {
> "Bar": [],
> "Baz": {},
> }
>
> The frontend guys complain because they can't just hand that structure to
> some of their libraries. I have been getting around this by defining
> custom marshallers for each type which detect empty or null items and
> marshal them out as '[]' and '{}' respectively. I know the other option is
> to just ensure an empty map/slice/array is initialized but invoking
> potential allocations and GC seems wasteful. Is there a json directive
> that can be attached that tells the standard library marshaller to always
> render empty items into a '[]', '{}', etc. similar to the *omitempty *
> directive?
>
> If not, would there be any support for me to propose, develop, and submit
> a patch to do so?
>
> As always, thank you all for the great work on Golang and the standard
> library!
> Kris
>
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.