Go - How do you change the value of a pointer parameter?
go, reflection
Solution
Not sure if this is what you were looking for, but yes you can change a pointer's value to something else. The code below will print 2 and 3:
package main
import (
"fmt"
)
func main() {
i := 1
testAsAny(&i)
fmt.Println(i)
testAsInt(&i)
fmt.Println(i)
}
func testAsAny(ptr interface{}) {
*ptr.(*int) = 2
}
func testAsInt(i *int) {
*i = 3
}
Problem
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 } ``` https://play.golang.org/p/3OwGYrb-W- Is it possible to have `test()` change `i` to point to another value 2?