title: How to access struct with a variable?
body: <p>I'm new to Go and I'm facing issues in accessing a struct with a variable</p>
  
  <p>I have this function <code>decodeUser</code>. My task is to check whether the keys are present in the request. So this is what I did. I've added a comment where I got the error.</p>
  
  <pre><code>func decodeUser(r *http.Request) (root.User, []string, error) {
      var u root.User
      if r.Body == nil {
          return u, []string{}, errors.New("no request body")
      }
      decoder := json.NewDecoder(r.Body)
      checks := []string{
          "UserName",
          "FirstName",
          "LastName",
          "Email",
      }
      emptyFields := []string{}
      for _, check := range checks {
          // i'm having problem over here `u[check]` it's showing (invalid
             operation: u[check] (type root.User does not support 
             indexing))
          if u[check] == nil {
              emptyFields = append(emptyFields, check)
          }
      }
      err := decoder.Decode(&amp;u)
      return u, emptyFields, err
  }
  </code></pre>
  
  <p>Just in case I added root.User here's structure for it</p>
  
  <pre><code>type User struct {
      ID                   string
      Username             string
      Password             string
      FirstName            string
      LastName             string
      Email                string
      PhoneNumber          string
      PhoneNumberExtension string
      DOB                  time.Time
      AboutMe              string
  }
  </code></pre>
  
  <p>The problem occurs as it doesn't allow me to access struct by a variable and I can't use this method which is <code>u.check</code>. So basically <strong>how should I make <code>u[check]</code> work</strong>?</p>