0

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!

1 Answer 1

1

You are attempting to set the value using a reflect.Value of a pointer to a reflect.Value, which is definitely not the same as a *[]string

Build the value up in steps and work your way outwards:

// create the []string, and set the element
slice := reflect.MakeSlice(t.Elem(), 1, 1)
slice.Index(0).SetString("USER")

// create the *[]string pointer, and set its value to point to the slice
ptr := reflect.New(slice.Type())
ptr.Elem().Set(slice)

// set the pointer in the struct
v.Elem().Set(ptr)

https://play.golang.org/p/Tj_XeXNNsML

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.