Optimizing structs

I use Json2Go to quickly create API response relevant structs.

Many times I find that the structs have large numbers of unused fields.

I’m wondering if there’s any tool to quickly eliminate unused struct fields from code.

Or does the compiler already ignore them?

As the are defined in the struct, they are always there, even unused, Go initializes them in its zero value (say int, 0, and so on)

I assume that you’re concerned with the unused struct fields when marshaling the response JSON object. Have you included the “omitempty” field tag in your struct definition? json package - encoding/json - Go Packages

Thanks for the responses. I wasn’t aware of the omitempty tag. That’s interesting.

I was hoping for an extension or website that could clean these up based on the fact that the fields are not called in any of the functions. I suppose it would make for a fun project to build one…

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.