vendredi 12 mai 2017

Go - How do you change the value of a pointer parameter?

In Golang, is it possible to change a pointer parameter's value to something else?

For example,

func main() {
    i := 1
    test(&i)
}

func test(ptr interface{}) {
    v := reflect.ValueOf(ptr)

    fmt.Println(v.CanSet()) // false
    v.SetInt(2) // panic
}

http://ift.tt/2pHnBXT

Is it possible to have test() change i to point to another value 2?





Aucun commentaire:

Enregistrer un commentaire