dimanche 12 janvier 2020

How to get a pointer to the value within interface{}

I am trying to make a function that receives interface{} and returns a pointer to the underlying value.

NOTICE: I do not want to return a *interface{} that holds within *x, but simply return a *int (within interface{}) that points to x within p.

func get_pointer_to_value_within_p(p interface{}) interface{}{

    // type of p == int
    tp := reflect.TypeOf(p)
    fmt.Println(tp)

    // type of &p == *interface{}
    tpp := reflect.TypeOf(&p)
    fmt.Println(tpp)

    v := reflect.ValueOf(p)

    // How to get from "v" a pointer to its value?
}

func main() {
    var x int = 4
    px := get_pointer_to_p(x) // should return "a pointer to x"
}

Thanks!





Aucun commentaire:

Enregistrer un commentaire