Access Struct inside Slice of Struct

Hi I will appreciate your help on this

I get Name and type from struct inside the struct but when the struct is on an slice I do not know how to access the name and type of the fields.
This is my code
package main
import (
“fmt”
“net/url”
“time”
“reflect”
“strings”
“strconv”

)
type TestStruct struct {

Children22 struct {// I can acess this one ( see code) 
		ID   string
		Name string
	}
Children23 []struct { // this is not struct it is slice of sruct
		ID1   string	// I would like to access this one
		Name1 string
	}
Nest struct {
	Children []struct {  // I would like to get to this one too
		ID2   string
		Name2 string
	}
}

}

func main() {

var t1 TestStruct
t1.InterfaceStruct = &InterfaceStruct{}
main := reflect.ValueOf(&t1)
err := GetField(main)
		if err != nil {
			fmt.Println("Error lev 1 = ", err)
		}
}

func GetField ( f reflect.Value ) error {

if f.Kind() != reflect.Ptr {
	fmt.Println("Error not a pointer  ")  
}
val := f.Elem()
lenght := val.NumField()
for i := 0; i < lenght; i++ {
	typefield := val.Type().Field(i)
	elementType := val.Type().Field(i).Type
	type_filed := val.Type().Field(i).Type.String()
	elemKind    := elementType.Kind()
	
	fmt.Println("typefield.Name = ", typefield.Name) 
	fmt.Println("elementType    = ", elementType) 
	fmt.Println("what kind =", elemKind )
	
	if elemKind  ==  reflect.Slice{
		fmt.Println("is a Slice")
		fmt.Println(" Slice type =", val.Field(i).Addr().Elem().Type() )
		// what kind of slice 
		if strings.Contains(type_filed ,"struct") {
		
			// I do not know how to address the struct inside the slice
			
		}
	}
	if elemKind  == reflect.Struct {  // check first for time and URL are Struct
		if type_filed != "time.Time" && type_filed != "url.URL" {
		fmt.Println("is a struct ")
		 //pass this to function recursive	as   reflect.Value
		newSt := val.Field(i).Addr() // this works fine 
		err := GetField(newSt) // recall the func to get the struct
			if err != nil {
				fmt.Println("Error = ", err)
			}
		}
	}
	if elemKind  ==  reflect.Map {
		fmt.Println("is a Map")
	}

fmt.Println(" ")

}
return nil

}

Thanks in advance

What is your end goal? If you turn your anonymous structs into named structs your life will be MUCH easier.

Your use of reflect seems otherwise misguided, at least from the code posted here.

Thanks for your replay. The goal is to take an struct like the one in my example and auto generate a Form
so I need field name and type to make the code generic I use reflect.

I hope this clarify.

I’d suggest letting the type know how to generate a form for itself, i.e. something like a func (t *Sometype) Form() Form and then just calling that. The type can use the Form() methods of its elements to delegate to, perhaps together with some utility function to generate a field for the basic types (int, string, etc).

1 Like

Thanks for your replay. That is a good idea Let me think about that, Thanks

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.