type Address struct { Street string City string } type Person struct { Name string Age int Address Address } person := Person{Name: "John", Age: 24, Address: Address{Street: "123 Main St", City: "Chicago"}} v := reflect.ValueOf(person) value := v.FieldByName("Name").Interface() // Value Cap fmt.Println(value.(string)) // Output: John
slice := []string{"a", "b", "c"} v := reflect.ValueOf(slice) value := v.Index(1).Interface() // Value Cap fmt.Println(value.(string)) // Output: bIn the above examples, we use the Value Cap method to convert the values held by reflect.Value objects into interfaces{}. This enables us to access and manipulate the values in a generic way. The package library used in these examples is Go Reflect.