title: Removing fields from struct or hiding them in JSON Response
body: <p>I've created an API in Go that, upon being called, performs a query, creates an instance of a struct, and then encodes that struct as JSON before sending back to the caller.  I'd now like to allow the caller to be able to select the specific fields they would like returned by passing in a &quot;fields&quot; GET parameter.</p>
  <p>This means depending on the fields value(s), my struct would change.  Is there any way to remove fields from a struct?  Or at least hide them in the JSON response dynamically?  (Note: Sometimes I have empty values so the JSON omitEmpty tag will not work here)  If neither of these are possible, is there a suggestion on a better way to handle this?</p>
  <p>A smaller version of the structs I'm using are below:</p>
  <pre><code>type SearchResult struct {
      Date        string      `json:&quot;date&quot;`
      IdCompany   int         `json:&quot;idCompany&quot;`
      Company     string      `json:&quot;company&quot;`
      IdIndustry  interface{} `json:&quot;idIndustry&quot;`
      Industry    string      `json:&quot;industry&quot;`
      IdContinent interface{} `json:&quot;idContinent&quot;`
      Continent   string      `json:&quot;continent&quot;`
      IdCountry   interface{} `json:&quot;idCountry&quot;`
      Country     string      `json:&quot;country&quot;`
      IdState     interface{} `json:&quot;idState&quot;`
      State       string      `json:&quot;state&quot;`
      IdCity      interface{} `json:&quot;idCity&quot;`
      City        string      `json:&quot;city&quot;`
  } //SearchResult
  
  type SearchResults struct {
      NumberResults int            `json:&quot;numberResults&quot;`
      Results       []SearchResult `json:&quot;results&quot;`
  } //type SearchResults
  </code></pre>
  <p>I then encode and output the response like so:</p>
  <pre><code>err := json.NewEncoder(c.ResponseWriter).Encode(&amp;msg)
  </code></pre>