mercredi 27 septembre 2017

Get Interface Field Values From an Interface Without Declaring Structure in Golang

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.

The only thing I need is that I need to get the field value of the interface. Here is my code:

s := reflect.ValueOf(t)
    for i := 0; i < s.Len(); i++ {
        fmt.Println(s.Index(i))
    }

where t is an interface having following values:

"total_record": {
            "id": "null",
            "count": 1
        }

I want value of "count" like simply 1.

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?





Aucun commentaire:

Enregistrer un commentaire