title: Get Interface Field Values From an Interface Without Declaring Structure in Golang
body: <p>I am trying to get field values from an interface in Golang. The interface is initially an empty interface which is getting its values from a database result. The DB query is working fine.</p>
  
  <p>The only thing I need is that I need to get the field value of the interface.
  Here is my code:</p>
  
  <pre><code>s := reflect.ValueOf(t)
      for i := 0; i &lt; s.Len(); i++ {
          fmt.Println(s.Index(i))
      }
  </code></pre>
  
  <p>where t is an interface having following values:</p>
  
  <pre><code>map[id:null count:1]
  </code></pre>
  
  <p>I want value of <code>"count"</code> like simply 1.</p>
  
  <p>My problem is that the Index() method is returning a panic because it needs a struct and I dont have any struct here. So what should I do to get interface value? Is there any solution to iterate over an interface to get field values with or without Golang's reflection package?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>After getting the value for count I need to parse it to json.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>type ResponseControllerList struct{
      Code            int             `json:"code"`
      ApiStatus       int             `json:"api_status"`
      Message         string          `json:"message"`
      Data            interface{}     `json:"data,omitempty"`
      TotalRecord     interface{}     `json:"total_record,omitempty"`
  }
  response := ResponseControllerList{}
  ratingsCount := reflect.ValueOf(ratingsCountInterface).MapIndex(reflect.ValueOf("count"))
  fmt.Println(ratingsCount)
  
  response = ResponseControllerList{
                  200,
                  1,
                  "success",
                  nil,
                  ratingsCount,
              }
  GetResponseList(c, response)
  
  func GetResponseList(c *gin.Context, response ResponseControllerList) {
      c.JSON(200, gin.H{
          "response": response,
      })
  }
  </code></pre>
  
  <p>The above code is being used to get the <code>ratingCount</code> in JSON format to use this response as API response. In this code, I am using the GIN framework to make HTTP request to API.</p>
  
  <p>Now the problem is that when I am printing the variable <code>ratingsCount</code>, its displaying the exact value of count in terminal what I need. But when I am passing it to JSON, the same variable gives me the response like:</p>
  
  <pre><code>{
      "response": {
          "code": 200,
          "api_status": 1,
          "message": "Success",
          "total_record": {
              "flag": 148
          }
      }
  }
  </code></pre>
  
  <p>What is the way to get the count's actual value in JSON ?</p>