lundi 5 octobre 2020

setting a nil pointer to a slice in a struct using reflection

I'm practicing reflections using go and I'm trying to achieve the following, to have a struct type, with a field that is a pointer to a slice of strings.

now that pointer is nil, i want to create the slice, add a value and set that pointer in the struct to point to the newly created slice, and to do all of that using reflections.

I created the following sample code to demonstrate what I'm doing:

package main

import (
    "log"
    "reflect"
)

type UserInfo struct {
    Name   string
    Roles  *[]string
    UserId int
}


func main() {
    var myV UserInfo
    myV.Name="moshe"
    myV.UserId=5
    v := reflect.ValueOf(&myV.Roles)
    t := reflect.TypeOf(myV.Roles)
    myP := reflect.MakeSlice(t.Elem(),1,1)
    myP.Index(0).SetString("USER")
    v.Elem().Set(reflect.ValueOf(&myP)) <-- PANIC HERE
    log.Print(myV.Roles)
}

this panics the message

panic: reflect.Set: value of type *reflect.Value is not assignable to type *[]string

of course the slice does not create a pointer so if i do this:

v.Elem().Set(myP.Convert(v.Elem().Type()))

I get

panic: reflect.Value.Convert: value of type []string cannot be converted to type *[]string

but when I try to convert the address with

v.Elem().Set(reflect.ValueOf(&myP).Convert(v.Elem().Type()))

I get

panic: reflect.Value.Convert: value of type *reflect.Value cannot be converted to type *[]string

what am I still missing?

thanks!





Aucun commentaire:

Enregistrer un commentaire