How to dynamically deref a pointer?

I am looking for an answer to this one:

the question says:

I have this func:

func getStringRepresentation(v interface{}, size int, brk bool, depth int) string {

    val := reflect.ValueOf(v)

    if val.Kind() == reflect.Ptr {
        v = *v.(types.Pointer)   // does not work
        val = reflect.ValueOf(v)
    }

   // ...

}

how can I dereference the pointer to get the value of it? When I use:

v = *v.(types.Pointer)

The error says:

Invalid indirect of ‘v.(types.Pointer)’ (type ‘types.Pointer’)

I think what you’re looking for is:

func getStringRepresentation(v interface{}, size int, brk bool, depth int) string {

    val := reflect.ValueOf(v)

    if val.Kind() == reflect.Ptr {
        val = val.Elem()
    }

   // ...

}
1 Like