title: Validate struct field if it exists
body: <p>I'm POSTing a JSON user object to my Golang application where I decode the 'req.body' into a 'User' struct.</p>
  
  <pre><code>err := json.NewDecoder(req.Body).Decode(user)
  //handle err if there is one
  </code></pre>
  
  <p>and the 'User' struct:</p>
  
  <pre><code>type User struct {
      Name      string  `json:"name,omitempty"`
      Username  string  `json:"username,omitempty"`
      Email     string  `json:"email,omitempty"`
      Town      string  `json:"town,omitempty"`
      //more fields here
  }
  </code></pre>
  
  <p>While I don't need help with the actual validation, I would like know how to validate usernames only if it is included as part of the JSON object. At the moment, if a username isn't included then <code>User.Username</code> will still exist but be empty i.e. <code>""</code></p>
  
  <p><strong>How can I check to see if '"username"' was included as part of the POSTed object?</strong></p>