Is it possibile to obtain a reference to an Interface Value without reccurring to reflection? If not, why not?
I have attempted:
package foo
type Foo struct {
a, b int
}
func f(x interface{}) {
var foo *Foo = &x.(Foo)
foo.a = 2
}
func g(foo Foo) {
f(foo)
}
but it fails with:
./test.go:8: cannot take the address of x.(Foo)
To clear your doubts if you go by the meaning Assert
state a fact or belief confidently and forcefully
in your example x.(Foo)
is just a type assertion it's not a object so you can not get its address.
so at runtime when the object will be created for example
var c interface{} = 5
d := c.(int)
fmt.Println("Hello", c, d) // Hello 5 5
it only assert that
asserts that c is not nil and that the value stored in c is of type int
So its not any physical entity in memory but at runtime d
will be allocated memory based on asserted type and than the contents of c will be copied into that location.
So you can do something like
var c interface{} = 5
d := &c
fmt.Println("Hello", (*d).(int)) // hello 5
Hope i cleared your confusion.