package main
import (
"fmt"
"reflect"
)
//AuthRequest struct
type AuthRequest struct {
Id int64
}
func main() {
//AuthRequest auth1
auth1 := AuthRequest{
Id : 1111,
}
//Authrequest auth2
auth2 := AuthRequest{
Id : 2222,
}
//create slice
var sliceModel = make([]AuthRequest, 0)
//put element to slice
sliceModel = append(sliceModel, auth1)
sliceModel = append(sliceModel, auth2)
//Pointer to an array
model := &sliceModel
//How do I get the struct Id field here?
v := reflect.ValueOf(model).Elem()
for j := 0; j < v.NumField(); j++ {
f := v.Field(j)
n := v.Type().Field(j).Name
t := f.Type().Name()
fmt.Printf("Name: %s Kind: %s Type: %s
", n, f.Kind(), t)
}
}
When I execute the above code, I get the following error.
panic: reflect: call of reflect.Value.NumField on slice Value [recovered]
How do I use reflection to traverse slice to get the value of Id field in the struct AuthRequest?