dimanche 26 septembre 2021

get reflect.struct from interface

hi a have this func for get type of value, but i try this and never can get reflect.struct:

type Test struct {
    Code int   
    Name string
}
func main(){
    test := getTest()
    data, err := getBytes(slice...)
    sanitizedFile := bytes.Split(data, []byte("\r\n"))
    err = Unmarshal(sanitizedFile[0], &test)
}
func getTest() interface{} {
    return Test{}
}

With this code i don't can get the reflecet.struct from v params in Unmarshall func

func Unmarshal(data []byte, v interface{}) error {
    rv := reflect.ValueOf(v)

    if rv.Kind() == reflect.Ptr {
        rvElem := rv.Elem()
        
        switch rvElem.Kind() {
        case reflect.Struct:
           // implement me
        }
    }
    return ErrInvalid
}

I would like to know if I can somehow find out if an interface is of type struct or access the values ​​of that struct.





Aucun commentaire:

Enregistrer un commentaire